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
Showing
5 changed files
with
214 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
...wapping Nodes in a Linked List/15- Swapping Nodes in a Linked List ( Ibrahim Khalid ).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,29 @@ | ||
// Author : Ibarhim Khalid | ||
class Solution { | ||
public: | ||
ListNode* swapNodes(ListNode* head, int k) { | ||
// to store values | ||
vector<int>v; | ||
int cnt=0; | ||
ListNode *temp = head; | ||
// move between values in linkedlist and store it in vector | ||
while(temp !=NULL){ | ||
v.push_back(temp->val); | ||
temp=temp->next; | ||
} | ||
// swapping the values of the kth node from the beginning and the kth node from the end | ||
swap(v[k-1],v[v.size()-k]); | ||
// pointer to index in vector | ||
int in=0; | ||
ListNode *tmp = head; | ||
// reorder linkedlist by modified vector | ||
while(tmp !=NULL){ | ||
tmp->val=v[in]; | ||
cout<<tmp->val<<" "; | ||
tmp=tmp->next; | ||
in++; | ||
} | ||
|
||
return head; | ||
} | ||
}; |
31 changes: 31 additions & 0 deletions
31
...5- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List (Ahmed Hossam).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,31 @@ | ||
// Author: Ahmed Hossam | ||
|
||
class Solution { | ||
public: | ||
ListNode* swapNodes(ListNode* head, int k) { | ||
// Initialize pointers to the head of the list | ||
ListNode* one = head; | ||
ListNode* two = head; | ||
ListNode* curr = head; | ||
|
||
// Calculate the size of the list | ||
int sz = 0; | ||
while (curr) | ||
sz++, curr = curr -> next; | ||
|
||
// Move 'one' pointer to the kth node from the beginning | ||
for (int i = 1; i < k; i++) | ||
one = one -> next; | ||
|
||
// Move 'two' pointer to the kth node from the end | ||
for (int i = 1; i < sz - k + 1; i++) | ||
two = two -> next; | ||
|
||
// Swap the values of the two nodes | ||
swap(one -> val, two -> val); | ||
|
||
// Return the updated head of the list | ||
return head; | ||
} | ||
|
||
}; |
59 changes: 59 additions & 0 deletions
59
.../15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List (Omar Sanad).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,59 @@ | ||
// author : Omar Sanad | ||
|
||
class Solution { | ||
public: | ||
ListNode* swapNodes(ListNode* head, int k) { | ||
|
||
// declare two Nodes one pointing to k_th node from the beginning | ||
// the other pointing to k_th node from the end | ||
ListNode *FRONT, *BACK; | ||
|
||
// initialize a variable sz to store the length of the linked list | ||
int sz = 0; | ||
|
||
// intialize a variable "curr" to keep track the of the curr node while calculating the length of the linked list | ||
ListNode* curr = head; | ||
while (curr != NULL) | ||
curr = curr->next, sz++; | ||
|
||
// initialize a variable "i" to keep track of the idx of the current node ..... | ||
// while trying to select the k_th node from the beginning and the k_th node from the end | ||
int i = 1; | ||
|
||
// update the value of curr to reuse it again while iterating over the linked list | ||
curr = head; | ||
|
||
// iterate over the linked list to select the k_th from beginning and the k_th from end | ||
while (curr != NULL) { | ||
|
||
// if the current idx == k, then this is the k_th from beginning | ||
if (i == k) | ||
FRONT = curr; | ||
|
||
// if the current idx == sz - k + 1, then this is the k_th from the end | ||
if (i == sz - k + 1) | ||
BACK = curr; | ||
|
||
// every iteration we update the curr and i | ||
curr = curr->next; | ||
i++; | ||
} | ||
|
||
// swap the values of the two nodes | ||
swap(FRONT->val, BACK->val); | ||
|
||
// return the given list after modifing if | ||
return head; | ||
} | ||
}; | ||
|
||
/** | ||
* 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) {} | ||
* }; | ||
*/ |
46 changes: 46 additions & 0 deletions
46
...5- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List (Osama Ayman).java
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,46 @@ | ||
// Author: Osama Ayman | ||
// Time: O(n) | ||
// Space: O(1) | ||
/** | ||
* Definition for singly-linked list. | ||
* public class ListNode { | ||
* int val; | ||
* ListNode next; | ||
* ListNode() {} | ||
* ListNode(int val) { this.val = val; } | ||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; } | ||
* } | ||
*/ | ||
class Solution { | ||
public ListNode swapNodes(ListNode head, int k) { | ||
ListNode cur = head, first = head; | ||
int size = 1; | ||
|
||
// finding linked list size | ||
while(cur != null){ | ||
// first node | ||
if(size == k){ | ||
first = cur; | ||
} | ||
size++; | ||
cur = cur.next; | ||
} | ||
int cnt = 1; | ||
cur = head; | ||
// finding second node that its value needs to be swapped with that of the | ||
// first node | ||
while(cnt <= size-k){ | ||
|
||
// we have found the second node, no need to increment cur again | ||
if(cnt == size-k) break; | ||
cur = cur.next; | ||
cnt++; | ||
} | ||
System.out.println(first.val + " " + cur.val); | ||
// swapping | ||
int tmp = cur.val; | ||
cur.val = first.val; | ||
first.val = tmp; | ||
return head; | ||
} | ||
} |
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