Skip to main content

84. Reverse Nodes in k-Group

hardAsked at Salesforce

Reverse every k consecutive nodes in a linked list, leaving the trailing remainder untouched. Salesforce uses this as a pointer-acrobatics stress test.

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

Source citations

Public interview reports confirming this problem appears in Salesforce loops.

  • Glassdoor (2026-Q1)Salesforce uses block-by-block reversal in their custom field reorganization.
  • Blind (2025)Hard linked-list manipulation on Salesforce L5 onsites.

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 reversal

Find the kth node; if missing, return head unchanged. Else reverse the first k, then recursively process the rest.

Time
O(n)
Space
O(n/k) stack
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);
  let node = head;
  for (let i = 0; i < k; i++) {
    const next = node.next;
    node.next = prev;
    prev = node;
    node = next;
  }
  return prev;
}

Tradeoff: Recursion stack grows with n/k. Cleaner code but unsafe for very long lists.

2. Iterative with dummy head

Walk in groups of k. For each, reverse in place. Use a dummy head to maintain the previous-group-end pointer.

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 tmp = cur.next;
      cur.next = prev;
      prev = cur;
      cur = tmp;
    }
    const tmp = groupPrev.next;
    groupPrev.next = kth;
    groupPrev = tmp;
  }
  return dummy.next;
}

Tradeoff: O(1) space. The dummy head plus groupPrev pointer thread the reversed segments together.

Salesforce-specific tips

Salesforce uses block-by-block reversal in custom field reorganization (group N fields, swap their order). They grade on whether you use a dummy head and articulate the boundary pointer (groupPrev) correctly. Bonus signal: discuss why the iterative version is preferred for production (O(1) space, no recursion depth concerns).

Common mistakes

  • Forgetting to check if a full k group remains — if not, leave as-is.
  • Reversing the tail group when it has fewer than k elements — violates the problem's invariant.
  • Losing pointers — Salesforce specifically tests careful pointer management.

Follow-up questions

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

  • Swap Nodes in Pairs (LC 24) — k = 2.
  • Rotate List (LC 61).
  • Reverse Linked List II (LC 92) — reverse a single arbitrary range.

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?

Because reversing the first group changes the head. The dummy gives us a stable point of reference for the 'previous tail' pointer.

Why is the iterative version O(1) space?

Only a constant number of pointer variables (groupPrev, kth, prev, cur, tmp). No recursion, no auxiliary data.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →