Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
7oSkaaa committed May 15, 2023
2 parents 3082a9a + efda285 commit fa4178a
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 0 deletions.
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;
}
};
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;
}

};
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) {}
* };
*/
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;
}
}
49 changes: 49 additions & 0 deletions 05- May/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
1. **[Solving Questions With Brainpower](#12--solving-questions-with-brainpower)**
1. **[Count Ways To Build Good Strings](#13--count-ways-to-build-good-strings)**
1. **[Maximize Score After N Operations](#14--maximize-score-after-n-operations)**
1. **[Swapping Nodes in a Linked List](#15--swapping-nodes-in-a-linked-list)**

<hr>
<br><br>
Expand Down Expand Up @@ -794,4 +795,52 @@ public:
}
};
```

<hr>
<br><br>

## 15) [Swapping Nodes in a Linked List](https://leetcode.com/problems/swapping-nodes-in-a-linked-list/)

### Difficulty

![](https://img.shields.io/badge/Medium-orange?style=for-the-badge)

### Related Topic

`Linked List` `Two Pointers`

### Code


```cpp
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;
}

};
```

0 comments on commit fa4178a

Please sign in to comment.