From d0f0b31f061ffede8ce4149b16a3a39ee6e078e3 Mon Sep 17 00:00:00 2001 From: Vijay Shanker Sharma Date: Wed, 5 Jun 2024 15:30:36 +0530 Subject: [PATCH 1/3] Added solution for Swap Nodes in Pairs --- .../0000-0099/0024-swap-nodes-in-pairs.md | 160 ++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 dsa-solutions/lc-solutions/0000-0099/0024-swap-nodes-in-pairs.md diff --git a/dsa-solutions/lc-solutions/0000-0099/0024-swap-nodes-in-pairs.md b/dsa-solutions/lc-solutions/0000-0099/0024-swap-nodes-in-pairs.md new file mode 100644 index 000000000..0967daaf2 --- /dev/null +++ b/dsa-solutions/lc-solutions/0000-0099/0024-swap-nodes-in-pairs.md @@ -0,0 +1,160 @@ +--- +id: swap-nodes-in-pairs +title: Swap Nodes in Pairs (LeetCode) +difficulty: Medium +sidebar_label: 0024-SwapNodesInPairs +topics: + - Linked List +companies: [] +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. +--- + +## Problem Description + +| Problem Statement | Solution Link | LeetCode Profile | +| :---------------- | :------------ | :--------------- | +| [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/) | + + +## 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:** `head = [1,2,3,4]` +- **Output:** `[2,1,4,3]` +- **Explanation:** The linked list is `[1,2,3,4]`. After swapping pairs, it becomes `[2,1,4,3]`. + +#### Example 2 + +- **Input:** `head = []` +- **Output:** `[]` +- **Explanation:** The input linked list is empty, so the output is also empty. + +#### Example 3 + +- **Input:** `head = [1]` +- **Output:** `[1]` +- **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]. +- `0 <= Node.val <= 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. + +1. **Base Case:** + - If the current node or the next node is null, return the current node. + +2. **Swap Nodes:** + - Swap the current node with its next node. + - Recursively call the function on the remaining linked list. + +3. **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 O(n), where n is the number of nodes in the linked list, making it efficient for handling the given constraints. From c72f9956907919ef9ac81ce1784b1f9fc8ee1351 Mon Sep 17 00:00:00 2001 From: Vijay Shanker Sharma Date: Thu, 6 Jun 2024 06:42:01 +0530 Subject: [PATCH 2/3] Made changes according to PA --- .../0000-0099/0024-swap-nodes-in-pairs.md | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/dsa-solutions/lc-solutions/0000-0099/0024-swap-nodes-in-pairs.md b/dsa-solutions/lc-solutions/0000-0099/0024-swap-nodes-in-pairs.md index 0967daaf2..984183eb3 100644 --- a/dsa-solutions/lc-solutions/0000-0099/0024-swap-nodes-in-pairs.md +++ b/dsa-solutions/lc-solutions/0000-0099/0024-swap-nodes-in-pairs.md @@ -3,9 +3,6 @@ id: swap-nodes-in-pairs title: Swap Nodes in Pairs (LeetCode) difficulty: Medium sidebar_label: 0024-SwapNodesInPairs -topics: - - Linked List -companies: [] 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. --- @@ -24,26 +21,26 @@ Given a linked list, swap every two adjacent nodes and return its head. You must #### Example 1 -- **Input:** `head = [1,2,3,4]` -- **Output:** `[2,1,4,3]` -- **Explanation:** The linked list is `[1,2,3,4]`. After swapping pairs, it becomes `[2,1,4,3]`. +- **Input:** $head = [1,2,3,4]$ +- **Output:** $[2,1,4,3]$ +- **Explanation:** The linked list is $[1,2,3,4]$. After swapping pairs, it becomes $[2,1,4,3]$. #### Example 2 -- **Input:** `head = []` -- **Output:** `[]` +- **Input:** $head = []$ +- **Output:** $[]$ - **Explanation:** The input linked list is empty, so the output is also empty. #### Example 3 -- **Input:** `head = [1]` -- **Output:** `[1]` +- **Input:** $head = [1]$ +- **Output:** $[1]$ - **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]. -- `0 <= Node.val <= 100` +- $0 <= Node.val <= 100$ ### Approach @@ -63,7 +60,7 @@ To solve this problem, we can use a recursive approach. We swap each pair of nod #### Python -``` +```py class ListNode: def __init__(self, val=0, next=None): self.val = val @@ -97,7 +94,7 @@ class Solution: #### Java -``` +```java class Solution { public ListNode swapPairs(ListNode head) { // If the list is empty or has only one node, no need to swap @@ -126,7 +123,7 @@ class Solution { #### C++ -``` +```cpp class Solution { public: ListNode* swapPairs(ListNode* head) { @@ -157,4 +154,4 @@ public: ### 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 O(n), where n is the number of nodes in the linked list, making it efficient for handling the given constraints. +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. From be32244e15b90b5d5d4a118f6c40903f9ecfbb10 Mon Sep 17 00:00:00 2001 From: Ajay Dhangar <99037494+Ajay-Dhangar@users.noreply.github.com> Date: Thu, 6 Jun 2024 09:37:08 +0530 Subject: [PATCH 3/3] Removed `difficulty: Medium` Because this is not a part of our docs structure --- dsa-solutions/lc-solutions/0000-0099/0024-swap-nodes-in-pairs.md | 1 - 1 file changed, 1 deletion(-) diff --git a/dsa-solutions/lc-solutions/0000-0099/0024-swap-nodes-in-pairs.md b/dsa-solutions/lc-solutions/0000-0099/0024-swap-nodes-in-pairs.md index 984183eb3..6be6dfe62 100644 --- a/dsa-solutions/lc-solutions/0000-0099/0024-swap-nodes-in-pairs.md +++ b/dsa-solutions/lc-solutions/0000-0099/0024-swap-nodes-in-pairs.md @@ -1,7 +1,6 @@ --- id: swap-nodes-in-pairs title: Swap Nodes in Pairs (LeetCode) -difficulty: Medium sidebar_label: 0024-SwapNodesInPairs 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. ---