Example 1
ready- Input
head = [ 1, 2, 3, 4 ]- Expected
[ 2, 1, 4, 3 ]- Why
- both adjacent pairs trade places
A line of dancers changes places two at a time: first trades with second, third trades with fourth, and anyone left at the end keeps their spot.
A linked list is a chain of nodes where each node's next field links to the one
after it. Given its head, swap every pair of adjacent nodes and return the head of the
changed chain. Two nodes are adjacent when one immediately follows the other.
A few ground rules:
0 <= length <= 10_000-1_000_000_000 <= Node.val <= 1_000_000_000Hints
Stand just before two nodes and call them first and second. After the swap,
second should lead to first, and first should lead to whatever followed the
pair.
The first swap changes the head, which makes it awkwardly different from every later swap. Put a temporary node before the head so every pair has a predecessor.
Once a pair is relinked, the old first node is now the pair's tail. Make it your
predecessor for the next round.
Can you finish in one pass with O(1) extra space, changing only next pointers and
without building another list?
Visible cases
head = [
1,
2,
3,
4
][
2,
1,
4,
3
]head = [
1,
2,
3
][
2,
1,
3
]head = [][]Interview signal
Start with the most direct model of the result. Copy every value into an array, swap neighboring array entries, then build a fresh list in that order.
def swap_pairs(head: ListNode | None) -> ListNode | None:
values: list[int] = []
current = head
while current is not None:
values.append(current.val)
current = current.next
for i in range(0, len(values) - 1, 2):
values[i], values[i + 1] = values[i + 1], values[i]
dummy = ListNode()
tail = dummy
for value in values:
tail.next = ListNode(value)
tail = tail.next
return dummy.nextThis gets the returned order right, but it copies the entire list and replaces the original nodes. It gives you a trustworthy baseline before you tackle the links.
Time O(n) — read, swap, and rebuild at most n items each. Space O(n) for the array and the rebuilt list, excluding the returned nodes themselves.
A dummy node is a temporary node placed before the real head so the first pair and
every later pair follow the same rules. Keep previous just before the pair, then name
the next two nodes first and second.
Save second.next before changing anything. Then connect the three links in their new
order: previous → second → first → remainder.
def swap_pairs(head: ListNode | None) -> ListNode | None:
dummy = ListNode(0, head)
previous = dummy
while previous.next is not None and previous.next.next is not None:
first = previous.next
second = first.next
first.next = second.next
second.next = first
previous.next = second
previous = first
return dummy.nextAfter each round, first has become the tail of the swapped pair. Moving previous
there puts it directly before the next untouched pair. If fewer than two nodes remain,
the loop stops and an odd final node is already in the right place.
Time O(n) — each node is visited a constant number of times. Space O(1) — only a few pointers and one temporary dummy node are used.
Ready to run.
3 cases are queued.
Visible testcase
Case 1
Expected
[ 2, 1, 4, 3 ]