DSA Tracker

Problem

Pattern this teaches:Linked List Manipulation

Medium

Swap Nodes in Pairs

A medium Linked List interview guide. Task statement, worked examples, intuition, and step-by-step solutions.

Topic
Linked List
Sheets
1
Core for
4 roles

The problem

Walk a linked list two nodes at a time and exchange the positions of each pair, moving the nodes themselves rather than their values. If the list has an odd length, the last node stays where it is. Return the new first node.

Example 1

Input
[4,1,6,3,8]
Output
[1,4,3,6,8]

Example 2

Input
[]
Output
[]

Example 3

Input
[1]
Output
[1]

Constraints

  • 0 <= n <= 100
  • -100 <= Node.val <= 100

How to think about it

Updated 2026-09-29

A swap between two nodes affects three connections: the predecessor pointing into the pair, the link between the two nodes themselves, and the successor trailing after them. Anchoring with a dummy node ensures that reordering the very first pair uses the exact same pointer logic as every subsequent pair.

Approaches, worst first

  1. Recursive pairwise swap

    time O(n) · space O(n)

    Swap the first two nodes directly, and recursively link `first.next` to the result of swapping the remaining tail. Clean and concise, but allocates call stack frames proportional to list length.

  2. Iterative pointer rewiring with dummyWrite this one

    time O(n) · space O(1)

    Anchor with a dummy node before head and point `prev` to it. While `prev.next` and `prev.next.next` exist, label them `first` and `second`. Set `prev.next = second`, `first.next = second.next`, and `second.next = first`. Advance `prev` to `first` and repeat.

Where people lose marks · 3
  • Swapping node values instead of actual pointer links bypasses the mechanical requirement of the problem and fails if node identity matters.
  • Losing reference to `second.next` before assigning `second.next = first` leaves the remainder of the list permanently unreachable.
  • Lists with 0 or 1 node must return head immediately without attempting pair dereferences.

The theory behind it

Linked List — the ground this problem stands on. All Linked List problems

What Linked List is

A linked list is a chain of separate cargo cars connected by coupling hooks, scattered anywhere across memory rather than sitting in a tidy contiguous row. Each car, called a node, holds a single piece of data and a pointer directing traffic to the address of the next car in line. Because nodes connect only by directional links, jumping straight to the tenth car is impossible without walking past the first nine.

When to reach for it

Choose a linked list when a problem requires frequent insertions and deletions at known positions without shifting whole blocks of surrounding memory. Problems mentioning pointer splicing, reversing subsequences in place, merging sorted streams, or detecting cycles in linear chains strongly point here. It is ideal when total capacity is unpredictable and memory allocation must happen one individual node at a time.

How the pattern works

Think in terms of pointer rewiring before dereferencing. Keep a dummy head node pointing to the start of the list so modifications to the initial item do not require separate edge logic. Always save references to neighboring nodes into temporary variables before cutting or redirecting forward links. When diagnosing loops or locating middle nodes, advance two references simultaneously at differing velocities so traversal completes without supplementary storage.

More Linked List problems

Track in your role's order

Pick your target role and all 370 problems resequence to what that interview actually asks.

Start free

Problem set and role mapping as of .