Solution: Reverse Nodes in Even Length Groups
Let's solve the Reverse Nodes in Even Length Groups problem using the In-place Reversal of a Linked List pattern.
We'll cover the following
Statement#
Given the head of a linked list, the nodes in it are assigned to each group in a sequential manner. The length of these groups follows the sequence of natural numbers. Natural numbers are positive whole numbers denoted by
In other words:
The
node is assigned to the first group. The
and nodes are assigned to the second group. The
, , and nodes are assigned to the third group, and so on.
Your task is to reverse the nodes in each group with an even number of nodes and return the head of the modified linked list.
Note: The length of the last group may be less than or equal to 1 + the length of the second to the last group.
Constraints:
Number of nodes
Solution#
We need to reverse the groups in the linked list with an even number of nodes. For this, we use the in-place reversal of the linked list. The first task is to identify these groups, and once they are identified, we can safely reverse the nodes present in these groups. Once all nodes are processed, we return the head of the modified linked list.
Following is the algorithm we will follow to achieve the solution to this problem:
-
We first initialize two variables,
prevandgroup_len, to point to the current group’s previous node and the current group’s length, respectively. We will setgroup_lento 2 at the beginning, as the first node of the linked list cannot be reversed since its group’s length is odd, i.e., 1. -
Iterate the nodes of the linked list, and in each iteration, we set a variable
nodeto track the current node andnum_nodesto account for the number of nodes in the current group. -
Next, we check the current group and count the nodes in it using
num_nodes. If the current group’s length is odd, it means that the group needs to be skipped, so theprevpointer is moved to the current node to start the next group’s processing. -
For groups with an even number of nodes, we will follow this procedure to reverse the nodes:
-
We maintain three pointers, i.e.,
prev,curr, andreverseto point to the node immediately before the current group, to the first node of the current group, and to the next node of the current group, respectively. -
We loop over the length of the group to reverse the nodes. For each node in the group, we save the reference to the next node,
curr_next, since we will change curr’s next pointer. -
Next, make
curr.nextpoint to the previous nodereverse. This step effectively reverses the pointer direction. -
Move
reverseto the current nodecurrbecause it will be the next node in the reversed group. -
Then, move
currto the next node in the original ordercurr_nextto continue the reversal process. -
Once the whole group is reversed, we update the
prev.nextto the last node andprevto the first node of the current group.
-
We keep on doing this until all the nodes of the linked list have been processed. Let’s look at the illustration below to better understand the solution:
1 of 24
2 of 24
3 of 24
4 of 24
5 of 24
6 of 24
7 of 24
8 of 24
9 of 24
10 of 24
11 of 24
12 of 24
13 of 24
14 of 24
15 of 24
16 of 24
17 of 24
18 of 24
19 of 24
20 of 24
21 of 24
22 of 24
23 of 24
24 of 24
We can see the code of this solution below:
Reverse Nodes In Even Length Groups
Reverse Nodes in k-Group