24. Swap Nodes In Pairs - Explanation
Description
Swap the first two nodes of the singly linked list, then the next two nodes, and continue pair by pair. Return the new head. If the list has an odd number of nodes, leave the final node in place. Change the links between nodes rather than changing the values stored in them. Arrays in the examples and test inputs show node values from head to tail; [] represents an empty list.
Example 1:
Input: head = [4,7,2,9]
Output: [7,4,9,2]Explanation: Swap the nodes containing 4 and 7, then swap the nodes containing 2 and 9. The resulting chain is 7 -> 4 -> 9 -> 2.
Example 2:
Input: head = [8,3,5]
Output: [3,8,5]Explanation: Swap the first pair, containing 8 and 3. The final node containing 5 has no partner and stays at the end.
Example 3:
Input: head = []
Output: []Explanation: An empty list has no pairs to swap, so return an empty list.
Constraints:
- The list contains between
0and100nodes. 0 <= Node.val <= 100- Swap nodes by changing links; do not modify node values.
Topics
Prerequisites
Before attempting this problem, you should be comfortable with:
- Linked List Fundamentals - Understanding node structure, traversal, and pointer manipulation in singly linked lists
- In-Place Pointer Manipulation - Reversing or reordering nodes by changing next pointers without extra data structures
- Dummy Node Technique - Using a sentinel node to simplify edge cases when the head of the list changes
1. Convert To Array
Intuition
The simplest approach is to convert the linked list to an array, where swapping elements is straightforward using index-based access. Once we swap adjacent elements in the array, we rebuild the linked list connections. This trades space efficiency for implementation simplicity.
Algorithm
- Traverse the linked list and store all nodes in an array.
- Iterate through the array in steps of
2, swapping adjacent elements. - Reconnect all nodes by setting each node's
nextpointer to the following node in the array. - Set the last node's
nexttonulland return the first element as the new head.
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head:
return None
arr = []
cur = head
while cur:
arr.append(cur)
cur = cur.next
for i in range(0, len(arr) - 1, 2):
arr[i], arr[i + 1] = arr[i + 1], arr[i]
for i in range(len(arr) - 1):
arr[i].next = arr[i + 1]
arr[-1].next = None
return arr[0]Time & Space Complexity
- Time complexity:
- Space complexity:
2. Recursion
Intuition
We can think of the problem recursively: swap the first two nodes, then let recursion handle the rest. The first node should point to the result of swapping the remaining list (starting from the third node). The second node becomes the new head of this pair and points to the first node. This recursive structure naturally handles lists of any length.
Algorithm
- Base case: if the list is empty or has only one node, return the head as-is.
- Save references to the first node (
cur) and second node (nxt). - Recursively swap the sublist starting from the third node and connect it to
cur. - Point
nxttocur. - Return
nxtas the new head of this swapped pair.
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head or not head.next:
return head
cur = head
nxt = head.next
cur.next = self.swapPairs(nxt.next)
nxt.next = cur
return nxtTime & Space Complexity
- Time complexity:
- Space complexity: for recursion stack.
3. Iteration
Intuition
We can swap pairs in place by carefully managing pointers. A dummy node simplifies handling the head change. For each pair, we need to: save the reference to the next pair, reverse the current pair's pointers, and connect the previous node to the new first node of the swapped pair. Moving two nodes at a time ensures we process each pair exactly once.
Algorithm
- Create a dummy node pointing to the head, and initialize
prevto dummy andcurrto head. - While
currandcurr.nextboth exist:- Save the start of the next pair:
nxtPair = curr.next.next. - Identify the second node in the pair.
- Reverse the pair: point second to first, first to the next pair.
- Connect previous node to the new first (second node).
- Update
prevto becurrand movecurrtonxtPair.
- Save the start of the next pair:
- Return
dummy.next.
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode(0, head)
prev, curr = dummy, head
while curr and curr.next:
nxtPair = curr.next.next
second = curr.next
# Reverse this pair
second.next = curr
curr.next = nxtPair
prev.next = second
# Update pointers
prev = curr
curr = nxtPair
return dummy.nextTime & Space Complexity
- Time complexity:
- Space complexity:
Common Pitfalls
Losing Reference to the Next Pair
When swapping a pair of nodes, you must save a reference to the node after the pair (curr.next.next) before modifying any pointers. If you swap the current pair first, curr.next changes and you lose access to the remaining list. Always store nxtPair = curr.next.next at the beginning of each iteration.
Forgetting to Update the Previous Node's Next Pointer
After swapping a pair, the previous node (or dummy node for the first pair) must point to the new first node of the swapped pair. A common mistake is correctly swapping the pair internally but failing to connect it back to the rest of the list, resulting in a broken chain or lost nodes.
Not Handling Odd-Length Lists
When the list has an odd number of nodes, the last node has no partner to swap with and should remain in place. Your loop condition must check that both curr and curr.next exist before attempting a swap. Failing to check both conditions can cause null pointer exceptions or incorrect behavior on the final unpaired node.
Sign in to join the discussion