Skip to main content

91. Reverse Nodes in k-Group

hardAsked at Reddit

Reverse nodes of a linked list in k-groups. Reddit uses this to test linked-list manipulation under partial-reverse rules — relevant when re-ordering feed chunks in their ranked-page generation.

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

Source citations

Public interview reports confirming this problem appears in Reddit loops.

  • Glassdoor (2026-Q1)Reddit on-site linked-list hard.

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

Reverse first k. Recurse on the rest. Connect the tail of reversed to the recursive result.

Time
O(n)
Space
O(n/k) recursion
function reverseKGroup(head, k) {
  let cur = head, count = 0;
  while (cur && count < k) { cur = cur.next; count++; }
  if (count < k) return head;
  let prev = reverseKGroup(cur, k);
  cur = head;
  for (let i = 0; i < k; i++) {
    const next = cur.next;
    cur.next = prev;
    prev = cur;
    cur = next;
  }
  return prev;
}

Tradeoff: Cleanest. O(n/k) recursion stack.

2. Iterative with dummy head (optimal)

Group-reverse iteratively using a dummy head and a 'groupPrev' anchor.

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) break;
    const groupNext = kth.next;
    let prev = groupNext, cur = groupPrev.next;
    while (cur !== groupNext) {
      const next = cur.next;
      cur.next = prev;
      prev = cur;
      cur = next;
    }
    const tmp = groupPrev.next;
    groupPrev.next = kth;
    groupPrev = tmp;
  }
  return dummy.next;
}

Tradeoff: O(1) space. The 'groupPrev' anchor handles chaining cleanly.

Reddit-specific tips

Reddit interviewers expect both versions. Bonus signal: walk through pointer manipulation with a small diagram before coding. Discuss when to prefer recursive vs. iterative.

Common mistakes

  • Forgetting to check if a full group exists before reversing.
  • Not handling the tail of the reversed group (must connect to the next group).
  • Off-by-one in the for-loop bounds for the k-step traversal.

Follow-up questions

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

  • Swap nodes in pairs (LC 24) — k=2 special case.
  • Reverse a portion of a linked list (LC 92).
  • Rotate list (LC 61).

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Why dummy head?

Reversing the first group requires re-pointing the head. Dummy removes the special case.

What if remaining nodes < k?

Leave them as-is. The kth check at the top of each iteration handles this.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →