Quant Memo
Coding/●●●●●

Swap every two adjacent nodes

Given the head of a singly linked list, swap every two adjacent nodes and return the new head. You must swap the nodes themselves, not just their values. If the list has an odd length, the last node stays in place.

1 -> 2 -> 3 -> 4        becomes   2 -> 1 -> 4 -> 3
1 -> 2 -> 3             becomes   2 -> 1 -> 3

Do it in O(n)O(n) time and O(1)O(1) extra space by relinking pointers. Overwriting val fields is a shortcut the interviewer is testing you to avoid.

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions