~/DHRUVUpskilling
← board/DSA/In place reversal LinkedList/dsa-in-place-reversal-linkedlist-06
Revision 1·26 Sept

Reverse Nodes In Even Length Groups

DifficultyHard
PatternIn place reversal LinkedList
TrackDSA
tl;dr

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, ...).

full write-up

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, ...).

Reverse Nodes in Even Length Groups

Note: The beginning of the problem statement (explaining how the linked list is first split into groups) was not included in what was shared. Based on the rest of the content, the setup is: nodes in a linked list are split into groups, where each group is one node longer than the group before it. Feel free to share the missing intro so it can be added here.

In other words:

  • The 1st node is assigned to the first group.
  • The 2nd and 3rd nodes are assigned to the second group.
  • The 4th, 5th, and 6th nodes are assigned to the third group, and so on.

Your task is to reverse the nodes in each group that has 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-last group.

Constraints

  • 1 ≤ Number of nodes ≤ 500
  • 0 ≤ LinkedListNode.data

Examples

(The original source references an illustration image for this section, which isn't available here. Below is a worked example based on the problem's grouping logic.)

Example 1

Input: head = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Explanation:

  • Group 1 (length 1): [1] → odd length, stays the same.
  • Group 2 (length 2): [2, 3] → even length, reversed to [3, 2].
  • Group 3 (length 3): [4, 5, 6] → odd length, stays the same.
  • Group 4 (length 4): [7, 8, 9, 10] → even length, reversed to [10, 9, 8, 7].

Output: [1, 3, 2, 4, 5, 6, 10, 9, 8, 7]

Example 2

Input: head = [1, 2, 3, 4, 5]

Explanation:

  • Group 1 (length 1): [1] → odd length, stays the same.
  • Group 2 (length 2): [2, 3] → even length, reversed to [3, 2].
  • Group 3 (remaining nodes, length ≤ 3): [4, 5] → since this last group has an even length (2), it gets reversed to [5, 4].

Output: [1, 3, 2, 5, 4]

Solution

The main idea is to keep track of a group length (group), and go through the list one group at a time. For each group, we check if it has an even number of nodes. If it does, we reverse that group.

Steps

  • We start with a pointer called prev, pointing to the head of the list.
  • The first group always has an odd length (just 1 node), so we set our starting group length to 2 — this will be the length of the next group we look at.
  • For each group:
    • We move a node pointer forward, up to group times, counting how many nodes we actually moved through (numNode). This handles the case where the list runs out of nodes before reaching the full group length.
    • If numNode equals the expected group length, this group is complete, and we check if it should be reversed:
      • We remember where the group starts (the node right after prev).
      • We remember what comes after the group, so we can reconnect the list later.
      • We reverse the group's nodes.
      • We connect prev to the new start of the reversed group.
      • We connect the end of the reversed group (which was the original start) to whatever came after the group.
      • We move prev to the end of this now-reversed group, ready for the next one.
    • If the group is not complete (we ran out of nodes early), we stop.
  • After each group, we increase group by 1, since each group is one node longer than the last.
  • At the end, we return the (possibly modified) head of the list.

Code

var reverseEvenLengthGroups = function (head) {


    function reverseK(head, k) {
        let curr = head;
        let next = null;
        let prev = null;

        while (k > 0) {
            next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
            k--;

        }

        return prev;

    }

    let prev = head;
    let group = 2;

    while (prev.next !== null) {
        let node = prev;
        let nodeLen = 0;

        for (let i = 0; i < group; i++) {
            if (node.next === null) {
                break;
            } else {
                node = node.next;
                nodeLen++;
            }
        }

        if (nodeLen % 2) {

            prev = node;
        } else {

            let nextGroup = node.next;
            let groupStart = prev.next;
            let reversedHead = reverseK(groupStart, nodeLen);
            prev.next = reversedHead;
            groupStart.next = nextGroup;
            prev = groupStart;


        }

        group++

    }
    return head;
};
Reverse Nodes In Even Length Groups — pattern notes — Dhruv Parmar