Reverse a Linked List in groups of given size Given a linked list, write a function to reverse every k nodes (where k is an input to the function). Example: Inputs: 1->2->3->4->5->6->7->8->NULL and k = 3 Output: 3->2->1->6->5->4->8->7->NULL. Inputs: 1->2->3->4->5->6->7->80->NULL and k = 5 Output: 5->4->3->2->1->8->7->6->NULL. Algorithm: reverse(head, k) 1) Reverse the first sub-list of size k. While reversing keep track of the next node and previous node. Let the pointer to the next node be next and pointer to the previous node be prev. See this post for reversing a linked list.
Read full article from Reverse a Linked List in groups of given size | GeeksforGeeks
No comments:
Post a Comment