Skip to main content

39. Swap Nodes in Pairs

mediumAsked at Plaid

Swap every two adjacent nodes in a linked list. Plaid asks this to verify pointer-juggling fluency before harder list-restructuring problems on chronological transaction sequences.

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

Source citations

Public interview reports confirming this problem appears in Plaid loops.

  • LeetCode Discuss (2026)Plaid backend OA.
  • Glassdoor (2025)Plaid SWE II screen.

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
[]

Approaches

1. Swap values

Walk the list two at a time and swap node values.

Time
O(n)
Space
O(1)
function swapPairs(head) {
  for (let p = head; p && p.next; p = p.next.next) {
    [p.val, p.next.val] = [p.next.val, p.val];
  }
  return head;
}

Tradeoff: Disallowed by the prompt — 'without modifying values.' Mention only as the warm-up.

2. Iterative node swap with dummy

Dummy head. For each pair, splice: prev -> second -> first -> rest.

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

Tradeoff: Three pointer reassignments per pair. The dummy head removes the special case for the first swap.

Plaid-specific tips

Plaid grades this on whether you respect the 'no value swap' constraint, which is the realistic constraint when nodes carry transaction IDs that must stay anchored to their original positions. Bonus signal: name each pointer reassignment out loud while drawing.

Common mistakes

  • Swapping values when the prompt forbids it — read carefully.
  • Forgetting to update prev.next — leaves prev pointing at the old first node.
  • Advancing prev to first.next instead of first — skips a pair.

Follow-up questions

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

  • Reverse nodes in k-group (LC 25) — generalization.
  • Recursive swap version.
  • Swap pairs in a doubly-linked list.

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 not just swap values?

In production, nodes often carry identity (transaction ID, timestamp). Swapping values is fine for LeetCode-pure-data; in real ledger code you swap node positions to keep identity intact.

Recursive vs iterative?

Recursive is shorter but pays O(n) stack. For 10^4-node transaction lists, iterative is safer.

Practice these live with InterviewChamp.AI

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