BackendML

Reverse Nodes in k-Group

Hard3 min read
The Reverse Nodes in k-Group problem asks us to reverse the nodes of a linked list in groups of k nodes while keeping the remaining nodes unchanged if fewer than k nodes are available.

Problem

Given the head of a linked list and an integer k, reverse the nodes of the linked list k at a time and return the modified list.

If the number of nodes remaining at the end is less than k, those nodes should remain in their original order.

The solution must modify the linked list by changing pointers and should not change the values stored in the nodes.

Example(s)

Consider the following example(s) to understand the expected input and output.

Input

head = [1, 2, 3, 4, 5]
k = 2

Output

[2, 1, 4, 3, 5]

Solution

This solution uses Linked List Reversal and Pointer Manipulation. We process the list one group at a time and reverse exactly k nodes.

First, we use a dummy node before the head to simplify handling the first group. For every group, we first check whether at least k nodes are available. If fewer than k nodes remain, we stop and leave them unchanged.

Once a complete group is found, we reverse the k nodes by changing their next pointers. After reversing the group, we connect it with the previous and next parts of the list.

The process continues until fewer than k nodes remain. Since the nodes are reversed in-place, no additional list nodes are created.
class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {

        ListNode dummy = new ListNode(0);
        dummy.next = head;

        ListNode groupPrev = dummy;
        while (true) {

            // Find the kth node of the current group.
            ListNode kth = getKthNode(groupPrev, k);

            if (kth == null) {
                break;
            }

            ListNode groupNext = kth.next;

            // Reverse the current group.
            ListNode prev = groupNext;
            ListNode current = groupPrev.next;

            while (current != groupNext) {
                ListNode next = current.next;
                current.next = prev;
                prev = current;
                current = next;
            }

            // Connect the reversed group.
            ListNode oldGroupStart = groupPrev.next;

            groupPrev.next = kth;

            // Move groupPrev to the end of the reversed group.
            groupPrev = oldGroupStart;
        }
        return dummy.next;
    }

    private ListNode getKthNode(ListNode start, int k) {
        ListNode current = start;

        for (int i = 0; i < k; i++) {
            if (current == null) {
                return null;
            }
            current = current.next;
        }
        return current;
    }
}

Complexity

Each node is visited a constant number of times while finding groups and reversing them. Therefore, the time complexity is O(n), where n is the number of nodes in the linked list.

The reversal is performed in-place using a constant number of pointers, so the extra space complexity is O(1).

Comments

No comments yet. Ask a question or share what helped.