Swap Nodes in Pairs (LeetCode)
Problem Descriptionβ
Problem Statement | Solution Link | LeetCode Profile |
---|---|---|
Merge Two Sorted Lists | Merge Two Sorted Lists Solution on LeetCode | VijayShankerSharma |
Problem Descriptionβ
Given a linked list, 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).
Examplesβ
Example 1β
- Input:
- Output:
- Explanation: The linked list is . After swapping pairs, it becomes .
Example 2β
- Input:
- Output:
- Explanation: The input linked list is empty, so the output is also empty.
Example 3β
- Input:
- Output:
- Explanation: There is only one node in the linked list, so no swapping occurs.
Constraintsβ
- The number of nodes in the list is in the range [0, 100].
Approachβ
To solve this problem, we can use a recursive approach. We swap each pair of nodes in the linked list by recursively swapping the remaining linked list after the current pair.
-
Base Case:
- If the current node or the next node is null, return the current node.
-
Swap Nodes:
- Swap the current node with its next node.
- Recursively call the function on the remaining linked list.
-
Return Head:
- Return the new head of the swapped pairs.
Solution Codeβ
Pythonβ
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def swapPairs(self, head):
# If the list is empty or has only one node, no need to swap
if not head or not head.next:
return head
# Dummy node to ease handling edge cases
dummy = ListNode(0)
dummy.next = head
prev = dummy
while prev.next and prev.next.next:
first = prev.next
second = prev.next.next
# Swapping
prev.next = second
first.next = second.next
second.next = first
# Move the pointer two steps forward
prev = first
return dummy.next
Javaβ
class Solution {
public ListNode swapPairs(ListNode head) {
// If the list is empty or has only one node, no need to swap
if (head == null || head.next == null) {
return head;
}
// Dummy node to ease handling edge cases
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode prev = dummy;
while (prev.next != null && prev.next.next != null) {
ListNode first = prev.next;
ListNode second = prev.next.next;
// Swapping
prev.next = second;
first.next = second.next;
second.next = first;
// Move the pointer two steps forward
prev = first;
}
return dummy.next;
}
}
C++β
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
// If the list is empty or has only one node, no need to swap
if (!head || !head->next) {
return head;
}
// Dummy node to ease handling edge cases
ListNode dummy(0);
dummy.next = head;
ListNode* prev = &dummy;
while (prev->next && prev->next->next) {
ListNode* first = prev->next;
ListNode* second = prev->next->next;
// Swapping
prev->next = second;
first->next = second->next;
second->next = first;
// Move the pointer two steps forward
prev = first;
}
return dummy.next;
}
};
Conclusionβ
The above solution effectively swaps every two adjacent nodes in a linked list without modifying the values in the nodes themselves. It utilizes a recursive approach to swap pairs of nodes, ensuring that the final linked list is correctly swapped. This solution has a time complexity of , where n is the number of nodes in the linked list, making it efficient for handling the given constraints.