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.

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 (1,2,3,4...)(1,2,3,4...).

In other words:

  • The 1st1^{st} node is assigned to the first group.

  • The 2nd2^{nd} and 3rd3^{rd} nodes are assigned to the second group.

  • The 4th4^{th}, 5th5^{th}, and 6th6^{th} 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:

  • 11 \leq Number of nodes 500\leq 500

  • 00 \leq LinkedListNode.data 103\leq 10^3

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:

  1. We first initialize two variables, prev and group_len, to point to the current group’s previous node and the current group’s length, respectively. We will set group_len to 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.

  2. Iterate the nodes of the linked list, and in each iteration, we set a variable node to track the current node and num_nodes to account for the number of nodes in the current group.

  3. 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 the prev pointer is moved to the current node to start the next group’s processing.

  4. 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, and reverse to 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.next point to the previous node reverse. This step effectively reverses the pointer direction.

    • Move reverse to the current node curr because it will be the next node in the reversed group.

    • Then, move curr to the next node in the original order curr_next to continue the reversal process.

    • Once the whole group is reversed, we update the prev.next to the last node and prev to 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:

Created with Fabric.js 3.6.6 We set a prev pointer pointing at the last node of the previous group. We set a variable group_len which indicates the length of the current group. group_len is initially set to2 because the very first node can't be reversed as it's contained in the first group of length 1 which is odd. Lastly, we keep a track of the successfully traversed nodes in the current group by the variable num_nodes, which is initially set to 0 as no nodes have been currently traversed. group_len = 2 num_nodes = 0

1 of 24

Created with Fabric.js 3.6.6 Since group_len = 2, we'll try to traverse the next two nodes of the linked list throughthe node pointer. We increment num_nodes along the way. group_len = 2 num_nodes = 0

2 of 24

Created with Fabric.js 3.6.6 We will keep traversing the current group nodes and increment num_nodes along the way. group_len = 2 num_nodes = 1

3 of 24

Created with Fabric.js 3.6.6 We have succesfully traversed all the nodes of the current group, i.e., group_len = 2.The value of num_nodes is also updated to 2. group_len = 2 num_nodes = 1

4 of 24

Created with Fabric.js 3.6.6 As num_nodes = 2, which is even, we will reverse these nodes through the curr and reverse pointers. Initially, curr points to the first node of the current group, i.e., prev.next and the reverse points to the first node of the next group, i,e., node.next. group_len = 2 num_nodes = 2

5 of 24

Created with Fabric.js 3.6.6 For each node in the current group, we set the next node of curr in a temporary pointer called curr_next. After this, we update the next node of curr to reverse. group_len = 2 num_nodes = 2

6 of 24

Created with Fabric.js 3.6.6 After this, we update the reverse pointer to curr. group_len = 2 num_nodes = 2

7 of 24

Created with Fabric.js 3.6.6 Next, we set curr to curr_next. The node 2 is reversed till here and we willrepeat the process for the rest of the nodes. group_len = 2 num_nodes = 2

8 of 24

Created with Fabric.js 3.6.6 We set the next node of curr in a temporary pointer called curr_next and set the next node of curr to reverse. group_len = 2 num_nodes = 2

9 of 24

Created with Fabric.js 3.6.6 After this, we update the reverse pointer to curr. group_len = 2 num_nodes = 2

10 of 24

Created with Fabric.js 3.6.6 Next, we set curr to curr_next. We have reversed the nodes of the current group. group_len = 2 num_nodes = 2

11 of 24

Created with Fabric.js 3.6.6 We set the next node of prev to the first node of this reversed group. group_len = 2 num_nodes = 2

12 of 24

Created with Fabric.js 3.6.6 We set prev equal to the last node of this reversed group. group_len = 2 num_nodes = 2

13 of 24

Created with Fabric.js 3.6.6 We have now reversed all the nodes in this even length group. We increment group_len by one and set num_nodes = 0 again to traverse the next group. group_len = 3 num_nodes = 0

14 of 24

Created with Fabric.js 3.6.6 Since group_len = 3, we'll try to traverse the next three nodes of the linked list throughthe node pointer. We increment num_nodes along the way. group_len = 3 num_nodes = 0

15 of 24

Created with Fabric.js 3.6.6 Since group_len = 3, we'll try to traverse the next three nodes of the linked list throughthe node pointer. We increment num_nodes along the way. group_len = 3 num_nodes = 1

16 of 24

Created with Fabric.js 3.6.6 We will keep traversing the current group nodes and increment num_nodes along the way. group_len = 3 num_nodes = 2

17 of 24

Created with Fabric.js 3.6.6 We will keep traversing the current group nodes and increment num_nodes along the way. group_len = 3 num_nodes = 3

18 of 24

Created with Fabric.js 3.6.6 As num_nodes = 3 which is odd, we don't reverse the nodes in this group. We increment group_len by one, set num_nodes = 0 again, and set prev equalto the last node of this group, i.e., node. group_len = 4 num_nodes = 0

19 of 24

Created with Fabric.js 3.6.6 Since group_len = 4, we'll try to traverse the next two nodes of the linked list throughthe node pointer. We increment num_nodes along the way. group_len = 4 num_nodes = 0

20 of 24

Created with Fabric.js 3.6.6 We will keep traversing the current group nodes and increment num_nodes along the way. group_len = 4 num_nodes = 1

21 of 24

Created with Fabric.js 3.6.6 We have succesfully traversed all the nodes of the current group. The valueof num_nodes is also updated to 2. group_len = 4 num_nodes = 2

22 of 24

Created with Fabric.js 3.6.6 We have reached the end of the linked list with num_nodes = 2. Even though it isn't equalto our desired group length of 4, since num_nodes is even, we'll still reverse the two nodes in this last group. We follow the same method as previously described to reverse the nodes in a group. group_len = 4 num_nodes = 2

23 of 24

Created with Fabric.js 3.6.6 After reversal of the nodes of the last group,we have this modified linked list with all the nodes of the even length groups reversed. group_len = 4 num_nodes = 2

24 of 24

We can see the code of this solution below:

main.py
linked_list_node.py
linked_list.py
print_list.py
Reverse Nodes In Even Length Groups

Time complexity#

The time complexity will be O(n)O(n), where nn is the number of nodes in the linked list.

Space complexity#

The space complexity will be O(1)O(1), since we use a constant number of additional variables to maintain the proper connections between the nodes during reversal.

Reverse Nodes In Even Length Groups

Reverse Nodes in k-Group