bool is_palin(struct node *head) { if (head == NULL || head->next == NULL) return true; struct node *slow = head; struct node *fast = head->next; struct node *prev_to_slow = NULL; while (fast && fast->next) { prev_to_slow = slow; slow = slow->next; fast = fast->next->next; } if (!fast) { struct node *second = slow->next; prev_to_slow->next = NULL; reverse_recursive(&second); bool result = compare_lists(head, second); reverse_recursive(&second); prev_to_slow->next = slow; return result; // the list is of odd lenth, should skip the middle element while comparing. } else { struct node *second = slow->next; slow->next = NULL; reverse_recursive(&second); bool result = compare_lists(head, second); reverse_recursive(&second); slow->next = second; return result; } return false; }
int main(){ NODE *head = NULL; int choice; struct node *cur = malloc(sizeof(NODE)); printf("Enter the data"); scanf("%d", &cur->data); cur->next = head; head = cur; do{ printf("Enter 1 to insert, 2 for display 3.to reverse,0.exit\n"); scanf("%d", &choice); switch(choice){ case 0: exit(0); break; case 1: insert(&head); break; case 2: display(head); break; case 3: reverse_recursive(&head); break; } }while(choice <= 3); return 0; }
data_t* reverse_recursive(data_t *head) { data_t *tmp; if (!head || !head->next) { return head; } tmp = reverse_recursive(head->next); head->next->next = head; head->next = NULL; return tmp; }
void reverse_recursive(struct node **head) { if (*head == NULL) return; struct node *result = (*head)->next; if (result == NULL) return ; reverse_recursive(&result); (*head)->next->next = *head; (*head)->next = NULL; *head = result; }
reverse_recursive (Node *cur, Node *prev, Node **Head) { if (cur->next == NULL) { *Head = cur; cur->next = prev; return; } Node *next = cur->next; cur->next = prev; reverse_recursive (next, cur, Head); }
void reverse_recursive(struct node** head) { struct node* first; struct node* remain; if (*head == NULL) return; first = *head; remain = first->next; if (remain == NULL) return; reverse_recursive(&remain); first->next->next = first; first->next = NULL; *head = remain; }
void reverse_recursive(node *head) { node *last=head,*slast; if(head->next==NULL) { return; } else { while(last->next!=NULL) { slast=last; last=last->next; } slast->next=NULL; last->next=slast; reverse_recursive(head); } return; }