Skip to main content

93. Reverse Nodes in k-Group

hardAsked at Datadog

Reverse linked list nodes in groups of K, leaving the tail intact if it's shorter than K. Datadog uses this to test deep pointer manipulation — same shape as in-place chunk reversal in their reverse-time-order query optimization.

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

Source citations

Public interview reports confirming this problem appears in Datadog loops.

  • Glassdoor (2026-Q1)Datadog onsite — deep linked-list manipulation.

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.

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 with k-step lookahead

Check if k nodes remain; reverse k; recurse for the rest.

Time
O(n)
Space
O(n/k) recursion
// Recursive: count k ahead; if ok, reverse k, link head.next to recurse(rest). Else return head.

Tradeoff: Uses recursion stack. Cleaner but Datadog often pushes for iterative.

2. Iterative with dummy head (optimal)

Use dummy + group_prev. For each group: verify k nodes; reverse them; rewire group_prev to new head and old head to next group_prev.

Time
O(n)
Space
O(1)
function reverseKGroup(head, k) {
  const dummy = { val: 0, 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;
    // reverse group_prev.next..kth
    let prev = groupNext;
    let curr = groupPrev.next;
    while (curr !== groupNext) {
      const next = curr.next;
      curr.next = prev;
      prev = curr;
      curr = next;
    }
    const tmp = groupPrev.next;
    groupPrev.next = kth;
    groupPrev = tmp;
  }
  return dummy.next;
}

Tradeoff: O(1) space. Datadog-canonical iterative form.

Datadog-specific tips

Datadog grades on whether you check for k nodes FIRST, before starting reversal. Without the lookahead, you can corrupt the tail. Articulate the group-prev rewire pattern BEFORE coding.

Common mistakes

  • Reversing without checking k-count — corrupts the tail when n%k != 0.
  • Forgetting to rewire group_prev.next AND old head's next — leaves orphan segments.
  • Off-by-one in the k-step counter — reverse k+1 or k-1 by mistake.

Follow-up questions

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

  • Swap Nodes in Pairs (LC 24) — k=2 special case.
  • Reverse Linked List II (LC 92) — between m and n.
  • Datadog-style: in-place chunk reversal during query reorder.

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 check k count first?

If fewer than k remain, the spec says don't reverse. Without checking, you'd partially reverse a fragment.

Recursive or iterative?

Iterative is preferred — O(1) space. Recursive uses O(n/k) stack.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →