Given a linked list, write a function to reverse every alternate k nodes (where k is an input to the function) in an efficient way. Give the complexity of your algorithm.
Method 1 (Process 2k nodes and recursively call for rest of the list)
Method 2 (Process k nodes and recursively call for rest of the list)
Read full article from Reverse alternate K nodes in a Singly Linked List | GeeksforGeeks
Method 1 (Process 2k nodes and recursively call for rest of the list)
struct node *kAltReverse(struct node *head, int k){ struct node* current = head; struct node* next; struct node* prev = NULL; int count = 0; /*1) reverse first k nodes of the linked list */ while (current != NULL && count < k) { next = current->next; current->next = prev; prev = current; current = next; count++; } /* 2) Now head points to the kth node. So change next of head to (k+1)th node*/ if(head != NULL) head->next = current; /* 3) We do not want to reverse next k nodes. So move the current pointer to skip next k nodes */ count = 0; while(count < k-1 && current != NULL ) { current = current->next; count++; } /* 4) Recursively call for the list starting from current->next. And make rest of the list as next of first node */ if(current != NULL) current->next = kAltReverse(current->next, k); /* 5) prev is new head of the input list */ return prev;}Method 2 (Process k nodes and recursively call for rest of the list)
Read full article from Reverse alternate K nodes in a Singly Linked List | GeeksforGeeks