Skip to main content

39. Swap Nodes in Pairs

mediumAsked at Datadog

Swap every two adjacent nodes in a linked list, in place. Datadog uses this to test pointer-manipulation discipline before harder variants like reverse-in-groups.

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

Source citations

Public interview reports confirming this problem appears in Datadog loops.

  • Glassdoor (2026-Q1)Datadog phone screen linked-list question.

Problem

Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed).

Constraints

  • The number of nodes in the list is in the range [0, 100].
  • 0 <= Node.val <= 100

Examples

Example 1

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

Example 2

Input
head = []
Output
[]

Example 3

Input
head = [1]
Output
[1]

Approaches

1. Swap values

Iterate; at each even position, swap val with prev.val.

Time
O(n)
Space
O(1)
// Swap node values; violates the constraint that only node positions change.

Tradeoff: Disallowed by the problem. Datadog will fail this for missing the explicit rule.

2. Iterative pointer rewiring (optimal)

Use dummy head. Track prev. For each pair (a, b), rewire: prev.next = b, a.next = b.next, b.next = a. Advance prev to a.

Time
O(n)
Space
O(1)
function swapPairs(head) {
  const dummy = { val: 0, next: head };
  let prev = dummy;
  while (prev.next && prev.next.next) {
    const a = prev.next;
    const b = a.next;
    a.next = b.next;
    b.next = a;
    prev.next = b;
    prev = a;
  }
  return dummy.next;
}

Tradeoff: O(1) extra space, no value mutation. Datadog-canonical: shows you can rewire pointers without losing references.

Datadog-specific tips

Datadog grades on whether you respect the 'no value change' constraint. They want pointer rewiring. Articulate the rewire order: capture both pointers FIRST, then swap. Recursive solutions are accepted but iterative is preferred for production-style code.

Common mistakes

  • Rewiring in the wrong order — losing a reference to b.next or a.
  • Forgetting the dummy head — fails when the list has at least 2 nodes (you'd need a special case for the new head).
  • Off-by-one in the while condition (prev.next && prev.next.next) — must check both to avoid null deref.

Follow-up questions

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

  • Reverse Nodes in K-Group (LC 25) — generalization to any K.
  • Reverse Linked List (LC 206) — same primitive.
  • Odd Even Linked List (LC 328) — different rewiring.

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 pointer rewiring instead of value swap?

The problem explicitly forbids value mutation. In production this matters when nodes carry payloads (object references, large strings) that are expensive to swap.

Recursive form?

Yes — swapPairs(head) = let next = head.next; head.next = swapPairs(next.next); next.next = head; return next. Same O(n) time but O(n) stack.

Practice these live with InterviewChamp.AI

Drill Swap Nodes in Pairs 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 →