forked from 7oSkaaa/LeetCode_DailyChallenge_2023
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bdb2a20
commit dca70a1
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
05- May/16- Swap Nodes in Pairs/16- Swap Nodes in Pairs (Ahmed Gamal).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Author: Ahmed Gamal | ||
|
||
// for this problem, we will use a recursive approach to solve it | ||
// we will swap the first two nodes, then we will call the function again | ||
|
||
// to achieve this, we need to some pointers | ||
// 1- head (function parameter): to point to the current node | ||
// 2- prv (function parameter): to point to the previous node of the current node (in the new linked list) | ||
// 3- next: to point to the next node of the current node | ||
// 4- next_next: to point to the next node of the next node of the current node | ||
|
||
// we will use the prv pointer to move the connection between the previous node and the current node to the next node | ||
// we will use the next_next pointer to move the connection between the current node and the next node to the next next node | ||
// then we will move the connection between the next_next node and the next node to the current node | ||
|
||
// we will call the function again with the next_next node as the current node and the current node as the previous node | ||
|
||
class Solution { | ||
void solve(ListNode* head, ListNode* prv = nullptr) { | ||
if(not(head and head -> next)) { | ||
return; | ||
} | ||
|
||
if(prv) { | ||
prv -> next = head -> next; | ||
} | ||
auto next = head -> next; | ||
auto next_next = next -> next; | ||
|
||
head -> next = next_next; | ||
next -> next = head; | ||
|
||
solve(next_next, head); | ||
} | ||
public: | ||
ListNode* swapPairs(ListNode* head) { | ||
if(not(head and head -> next)) | ||
return head; | ||
|
||
auto ret = head -> next; | ||
solve(head); | ||
|
||
return ret; | ||
} | ||
}; |