Linked List
Swap Nodes in Pairs
DESCRIPTION (inspired by Leetcode.com)
Given a reference head of type ListNode that is the head of a singly linked list, write a function to 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.)
Example 1: input:
output:
Explanation: 5 and 4 are swapped, 3 and 2 are swapped, and 1 is left alone.
Example 2: input:
output:
Explanation: 1 and 2 are swapped, 3 and 4 are swapped.
Solution
Need for a Dummy Node
def swapPairs(head):dummy = ListNode(0)dummy.next = headtail, first = dummy, headwhile first and first.next:second = first.next# swap nodestail.next = secondfirst.next = second.nextsecond.next = firsttail = firstfirst = first.nextreturn dummy.next
swap nodes in pairs
0 / 1
def swapPairs(head):dummy = ListNode(0)dummy.next = headtail, first = dummy, headwhile first and first.next:second = first.next# swap nodestail.next = secondfirst.next = second.nextsecond.next = firsttail = firstfirst = first.nextreturn dummy.next
create dummy node and initialize pointers
0 / 2
def swapPairs(head):dummy = ListNode(0)dummy.next = headtail, first = dummy, headwhile first and first.next:second = first.next# swap nodestail.next = secondfirst.next = second.nextsecond.next = firsttail = firstfirst = first.nextreturn dummy.next
swapped: 4 -> 5
0 / 3
def swapPairs(head):dummy = ListNode(0)dummy.next = headtail, first = dummy, headwhile first and first.next:second = first.next# swap nodestail.next = secondfirst.next = second.nextsecond.next = firsttail = firstfirst = first.nextreturn dummy.next
swapped: 2 -> 3
0 / 2
Termination
def swapPairs(head):dummy = ListNode(0)dummy.next = headtail, first = dummy, headwhile first and first.next:second = first.next# swap nodestail.next = secondfirst.next = second.nextsecond.next = firsttail = firstfirst = first.nextreturn dummy.next
0 / -1
Implementation
def swapPairs(head):# Create dummy node to simplify edge casesdummy = ListNode(0)dummy.next = headprev = dummy# Process pairs while both nodes existwhile prev.next and prev.next.next:# Identify the two nodes to swapfirst = prev.nextsecond = prev.next.next# Perform the swap by adjusting pointersprev.next = second # Link previous to second nodefirst.next = second.next # Link first to node after secondsecond.next = first # Link second to first (completing swap)# Move prev to the end of swapped pair for next iterationprev = firstreturn dummy.next # Return new head
Code
def swapPairs(head):dummy = ListNode(0)dummy.next = headtail, first = dummy, headwhile first and first.next:second = first.next# swap nodestail.next = secondfirst.next = second.nextsecond.next = firsttail = firstfirst = first.nextreturn dummy.next
swap nodes in pairs
0 / 10
Edge Cases
Empty List
def swapPairs(head):dummy = ListNode(0)dummy.next = headtail, first = dummy, headwhile first and first.next:second = first.next# swap nodestail.next = secondfirst.next = second.nextsecond.next = firsttail = firstfirst = first.nextreturn dummy.next
swap nodes in pairs
0 / 3
One Node
def swapPairs(head):dummy = ListNode(0)dummy.next = headtail, first = dummy, headwhile first and first.next:second = first.next# swap nodestail.next = secondfirst.next = second.nextsecond.next = firsttail = firstfirst = first.nextreturn dummy.next
swap nodes in pairs
0 / 4
Two Nodes
def swapPairs(head):dummy = ListNode(0)dummy.next = headtail, first = dummy, headwhile first and first.next:second = first.next# swap nodestail.next = secondfirst.next = second.nextsecond.next = firsttail = firstfirst = first.nextreturn dummy.next
swap nodes in pairs
0 / 7
Test Your Knowledge
Login to take the complexity quiz and track your progress
Complexity Analysis
Time Complexity: O(N) where N is the number of nodes in the list. We visit each node in the list once, and perform the same number of operations at each node.
Space Complexity: O(1) We only use a constant amount of extra space to store the pointers prev, first, and second.
Mark as read
Unlock Premium Coding Content
On This Page
Your account is free and you can post anonymously if you choose.