How to Reverse a Linked List in groups of given size?
One of the most significant data structure concepts to learn for your coding interviews is linked list!
A linked list is defined as a collection of certain data elements where each element points towards the next. Though, unlike arrays, linked lists are not stored at a contiguous memory location.
In the linked list arena, you may encounter a question on how to reverse a linked list in groups of given size!
Well, to help you find the answer for the same, we have curated this tutorial.
Here, learn all the details of reversing a link list alongside learning about reversing it using different approaches.
Let’s get started!
Reversing a linked list of K group
To understand the problem of reversing a linked list of group k, carefully understand this problem statement.
Given to you, the head of a linked list. Reverse all the nodes of the list k at any given time. Then, return it to its modified list.
K is our positive integer and is less than or equal to the length of a given linked list. If nodes are not a multiple of k, left out nodes should stay as it is.
You need not to alter the values of the list’s nodes as nodes themselves cannot be changed. Further, you can also sort 0 12 as the given values.
Look at the example:
1 – 2 – 3 – 4 -5
2 – 1 – 4 – 3 – 5
Input : head is [ 1,2,3,4,5]
Your k = 2
Output would be [ 2, 1, 4, 3, 5 ]
You have to first reverse the linked list of the sub-size k. Keep the track of all previous nodes and the pointers if you want to reverse your linked list.
Discussed Solution Approach
To know about reversing a linked list in a better way, you can have a look at the best solution approaches.
Reversing linked list using iteration or three pointer approach
This would surely be an ideal method if you want to reverse the linked list using iteration. One idea to reverse your linked list is to traverse it with the help of the loop or to change the next pointer of the node with the help of its previous nodes.
Linked list is defined as a linear data structure where each node will only point towards the next node in an ideal way. During this process, you can keep a track of the previous node with the help of some pointer in order to reverse a linked list list of groups of given size!
Before changing it to the next pointer of the current node, you can look at the other pointer to track your next node. Orderly you can use the traversal pointer for reversing a linked list or to sort 012.
You can use the three pointer linked list traversal wherein you can use one for tracking your current node, the other one for tracking the precious node and one for tracking of the next node.
By the end of the loop, you can return to the new head traversal loop.
Solution steps in this case would be:
- Initialise all the three pointers, prev as NULL, curr as your head and for also as NULL
- Then we will iterate through the linked list till the curr of our linked list will become null. You can keep changing your nodes from the next pointer to the previous nodes. During this process you can update to the curr, prev or forw pointers.
- Before changing of the next pointer in the curr node, store for node
- Then we will change the next pointer of our curr node and point it towards the prev
- Now the reversal of our curr node is done. So we can easily move to the prev and curr pointers as our one step forward
- Finally by the end of our loop, the curr node will automatically become NULL. The pre pointer will be at the last node which is considered as the head node or the new node of the given linked list. We will return prev pointer as our new head reference
At each iteration step, you can partially reverse the size of your linked list that will grow by one.
Time and space complexity
We will be exploring each node to perform a constant number of operations with each iteration. Time complexity in this case would be; n*O[1] = O [1]
As we are using the constant extra space so the space complexity here would be O[1]
Recursion or decrease and conquer approach to reverse a linked list
To reverse a linked list of groups of given size, you can solve the problem using recursion.
Suppose, length of a linked list n is given to you. You can divide the linked list in two parts; head node and remaining linked list of size n-1.
We can recursively reverse our remaining linked list of n-1 size by calling the exact function. It will return the head pointer of your liked list of n-1 size.
After the partial reversal, the head node will point towards the last node of your reversed linked list. In other words, the next node of head will become the last node of the reversed linked list of n-1 size. For full reversal, the head should be at the last node.
Time and space complexity analysis in this case
We will recursively solve the problem of n size with the sub problem of size n-1. The height of the recursion tree would be O[n] where the input size will automatically decrease by 1.
At each of the recursion stages, you can do constant operations. Time complexity in this case would be O[n]
Space complexity in this case would also be O[n]
Wrapping Up
We hope that this blog post has given you a better understanding on how to reverse a linked list list of groups of given size!
You can also recursively sort 0 1 2 in your linked list and gain an efficient insight on the said subject!
0