|
| 1 | +--- |
| 2 | +id: swap-nodes-in-pairs |
| 3 | +title: Swap Nodes in Pairs (LeetCode) |
| 4 | +sidebar_label: 0024-SwapNodesInPairs |
| 5 | +description: Swap every two adjacent nodes in a linked list and return its head. The values in the nodes must not be modified, only the nodes themselves can be changed. |
| 6 | +--- |
| 7 | + |
| 8 | +## Problem Description |
| 9 | + |
| 10 | +| Problem Statement | Solution Link | LeetCode Profile | |
| 11 | +| :---------------- | :------------ | :--------------- | |
| 12 | +| [Merge Two Sorted Lists](https://leetcode.com/problems/swap-nodes-in-pairs/) | [Merge Two Sorted Lists Solution on LeetCode](https://leetcode.com/problems/swap-nodes-in-pairs/solutions/) | [VijayShankerSharma](https://leetcode.com/u/darkknight648/) | |
| 13 | + |
| 14 | + |
| 15 | +## Problem Description |
| 16 | + |
| 17 | +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). |
| 18 | + |
| 19 | +### Examples |
| 20 | + |
| 21 | +#### Example 1 |
| 22 | + |
| 23 | +- **Input:** $head = [1,2,3,4]$ |
| 24 | +- **Output:** $[2,1,4,3]$ |
| 25 | +- **Explanation:** The linked list is $[1,2,3,4]$. After swapping pairs, it becomes $[2,1,4,3]$. |
| 26 | + |
| 27 | +#### Example 2 |
| 28 | + |
| 29 | +- **Input:** $head = []$ |
| 30 | +- **Output:** $[]$ |
| 31 | +- **Explanation:** The input linked list is empty, so the output is also empty. |
| 32 | + |
| 33 | +#### Example 3 |
| 34 | + |
| 35 | +- **Input:** $head = [1]$ |
| 36 | +- **Output:** $[1]$ |
| 37 | +- **Explanation:** There is only one node in the linked list, so no swapping occurs. |
| 38 | + |
| 39 | +### Constraints |
| 40 | + |
| 41 | +- The number of nodes in the list is in the range [0, 100]. |
| 42 | +- $0 <= Node.val <= 100$ |
| 43 | + |
| 44 | +### Approach |
| 45 | + |
| 46 | +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. |
| 47 | + |
| 48 | +1. **Base Case:** |
| 49 | + - If the current node or the next node is null, return the current node. |
| 50 | + |
| 51 | +2. **Swap Nodes:** |
| 52 | + - Swap the current node with its next node. |
| 53 | + - Recursively call the function on the remaining linked list. |
| 54 | + |
| 55 | +3. **Return Head:** |
| 56 | + - Return the new head of the swapped pairs. |
| 57 | + |
| 58 | +### Solution Code |
| 59 | + |
| 60 | +#### Python |
| 61 | + |
| 62 | +```py |
| 63 | +class ListNode: |
| 64 | + def __init__(self, val=0, next=None): |
| 65 | + self.val = val |
| 66 | + self.next = next |
| 67 | + |
| 68 | +class Solution: |
| 69 | + def swapPairs(self, head): |
| 70 | + # If the list is empty or has only one node, no need to swap |
| 71 | + if not head or not head.next: |
| 72 | + return head |
| 73 | + |
| 74 | + # Dummy node to ease handling edge cases |
| 75 | + dummy = ListNode(0) |
| 76 | + dummy.next = head |
| 77 | + prev = dummy |
| 78 | + |
| 79 | + while prev.next and prev.next.next: |
| 80 | + first = prev.next |
| 81 | + second = prev.next.next |
| 82 | + |
| 83 | + # Swapping |
| 84 | + prev.next = second |
| 85 | + first.next = second.next |
| 86 | + second.next = first |
| 87 | + |
| 88 | + # Move the pointer two steps forward |
| 89 | + prev = first |
| 90 | + |
| 91 | + return dummy.next |
| 92 | +``` |
| 93 | + |
| 94 | +#### Java |
| 95 | + |
| 96 | +```java |
| 97 | +class Solution { |
| 98 | + public ListNode swapPairs(ListNode head) { |
| 99 | + // If the list is empty or has only one node, no need to swap |
| 100 | + if (head == null || head.next == null) { |
| 101 | + return head; |
| 102 | + } |
| 103 | + // Dummy node to ease handling edge cases |
| 104 | + ListNode dummy = new ListNode(0); |
| 105 | + dummy.next = head; |
| 106 | + ListNode prev = dummy; |
| 107 | + while (prev.next != null && prev.next.next != null) { |
| 108 | + ListNode first = prev.next; |
| 109 | + ListNode second = prev.next.next; |
| 110 | + // Swapping |
| 111 | + prev.next = second; |
| 112 | + first.next = second.next; |
| 113 | + second.next = first; |
| 114 | + |
| 115 | + // Move the pointer two steps forward |
| 116 | + prev = first; |
| 117 | + } |
| 118 | + return dummy.next; |
| 119 | + } |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +#### C++ |
| 124 | + |
| 125 | +```cpp |
| 126 | +class Solution { |
| 127 | +public: |
| 128 | + ListNode* swapPairs(ListNode* head) { |
| 129 | + // If the list is empty or has only one node, no need to swap |
| 130 | + if (!head || !head->next) { |
| 131 | + return head; |
| 132 | + } |
| 133 | + |
| 134 | + // Dummy node to ease handling edge cases |
| 135 | + ListNode dummy(0); |
| 136 | + dummy.next = head; |
| 137 | + ListNode* prev = &dummy; |
| 138 | + |
| 139 | + while (prev->next && prev->next->next) { |
| 140 | + ListNode* first = prev->next; |
| 141 | + ListNode* second = prev->next->next; |
| 142 | + // Swapping |
| 143 | + prev->next = second; |
| 144 | + first->next = second->next; |
| 145 | + second->next = first; |
| 146 | + // Move the pointer two steps forward |
| 147 | + prev = first; |
| 148 | + } |
| 149 | + return dummy.next; |
| 150 | + } |
| 151 | +}; |
| 152 | +``` |
| 153 | +
|
| 154 | +### Conclusion |
| 155 | +
|
| 156 | +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 $O(n)$, where n is the number of nodes in the linked list, making it efficient for handling the given constraints. |
0 commit comments