Skip to main content

84. Reverse Nodes in k-Group

hardAsked at Plaid

Reverse the nodes of a linked list k at a time. Plaid asks this as an advanced pointer-juggling problem — the same shape they use when reordering chunks of a transaction-batch list during retry-coalescing.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Plaid loops.

  • Glassdoor (2025)Plaid SWE III onsite.
  • LeetCode Discuss (2026)Plaid hard list problem.

Problem

Given the head of a linked list, reverse the nodes of the list k at a time, and return the modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should remain as it is. You may not alter the values in the list's nodes, only nodes themselves may be changed.

Constraints

  • The number of nodes in the list is n.
  • 1 <= k <= n <= 5000
  • 0 <= Node.val <= 1000

Examples

Example 1

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

Example 2

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

Approaches

1. Recursive group reverse

If at least k nodes ahead, reverse them, then recurse on the rest.

Time
O(n)
Space
O(n/k) recursion
function reverseKGroup(head, k) {
  let node = head;
  for (let i = 0; i < k; i++) {
    if (!node) return head;
    node = node.next;
  }
  // node is the (k+1)th node
  let prev = reverseKGroup(node, k);
  let curr = head;
  for (let i = 0; i < k; i++) {
    const next = curr.next;
    curr.next = prev;
    prev = curr;
    curr = next;
  }
  return prev;
}

Tradeoff: Recursion depth = n/k. For very long lists, prefer iterative.

2. Iterative with group reverse

Dummy head. For each group of k: check k nodes ahead exist, then reverse in place and splice.

Time
O(n)
Space
O(1)
function reverseKGroup(head, k) {
  const dummy = { next: head };
  let groupPrev = dummy;
  while (true) {
    let kth = groupPrev;
    for (let i = 0; i < k && kth; i++) kth = kth.next;
    if (!kth) return dummy.next;
    const groupNext = kth.next;
    // reverse group
    let prev = groupNext, curr = groupPrev.next;
    while (curr !== groupNext) {
      const tmp = curr.next;
      curr.next = prev;
      prev = curr;
      curr = tmp;
    }
    const newGroupPrev = groupPrev.next;
    groupPrev.next = kth;
    groupPrev = newGroupPrev;
  }
}

Tradeoff: O(1) extra space. The dummy head and groupPrev/groupNext anchors handle the splice cleanly.

Plaid-specific tips

Plaid prefers iterative for production lists. Bonus signal: explicitly check that k nodes remain before reversing — partial trailing groups stay un-reversed per spec. Connect to retry-coalescing where you reverse the most recent batch but leave the trailing in-progress batch untouched.

Common mistakes

  • Reversing without first verifying k nodes ahead exist — produces a partially-reversed trailing group.
  • Losing the next-group anchor — the reverse loop runs to null, not the group boundary.
  • Forgetting to update groupPrev to the new tail of the just-reversed group.

Follow-up questions

An interviewer at Plaid may pivot to one of these next:

  • Reverse only when value satisfies a predicate.
  • Reverse in alternating groups of k.
  • Reverse a sub-section by node identity, not position.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

What if length isn't a multiple of k?

Per spec, the trailing partial group stays in its original order. The 'check kth exists' step gates the reverse.

Recursive or iterative?

Recursive is shorter; iterative is safer for long lists (no stack overflow). Plaid expects iterative for production code.

Practice these live with InterviewChamp.AI

Drill Reverse Nodes in k-Group and other Plaid interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →