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
041e726
commit 3082a9a
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
...15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List (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,53 @@ | ||
// Author: Ahmed Gamal | ||
|
||
// this is a simple implementation problem | ||
// we will use two pointers to point to the kth from the beginning and the kth from the end | ||
// then we will swap them | ||
// to get the kth from the end, we will use the size of the list - k + 1 | ||
// to get the size of the list, we will iterate over the list and count the number of nodes | ||
|
||
/** | ||
* Definition for singly-linked list. | ||
* struct ListNode { | ||
* int val; | ||
* ListNode *next; | ||
* ListNode() : val(0), next(nullptr) {} | ||
* ListNode(int x) : val(x), next(nullptr) {} | ||
* ListNode(int x, ListNode *next) : val(x), next(next) {} | ||
* }; | ||
*/ | ||
class Solution { | ||
public: | ||
ListNode* swapNodes(ListNode* head, int k) { | ||
// size: the size of the list | ||
// i: the kth node from the beginning | ||
// j: the kth node from the end | ||
int size = 0; | ||
ListNode *i; | ||
auto j = head; | ||
|
||
// we will iterate over the list and count the number of nodes | ||
while(j) { | ||
size++; | ||
|
||
// if we reached the kth node from the beginning, we will save it in i | ||
if(size == k) { | ||
i = j; | ||
} | ||
j = j -> next; | ||
} | ||
|
||
// we will get the kth node from the end (size - k + 1) | ||
size -= k; | ||
j = head; | ||
while(size--) { | ||
j = j -> next; | ||
} | ||
|
||
// we will swap the values of the two nodes | ||
swap(i -> val, j -> val); | ||
|
||
// we will return the head of the list | ||
return head; | ||
} | ||
}; |