diff --git a/05- May/15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List ( Ibrahim Khalid ).cpp b/05- May/15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List ( Ibrahim Khalid ).cpp new file mode 100644 index 000000000..e23c88580 --- /dev/null +++ b/05- May/15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List ( Ibrahim Khalid ).cpp @@ -0,0 +1,29 @@ +// Author : Ibarhim Khalid +class Solution { +public: + ListNode* swapNodes(ListNode* head, int k) { + // to store values + vectorv; + 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<val<<" "; + tmp=tmp->next; + in++; + } + + return head; + } +}; diff --git a/05- May/15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List (Ahmed Hossam).cpp b/05- May/15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List (Ahmed Hossam).cpp new file mode 100644 index 000000000..a108acf2a --- /dev/null +++ b/05- May/15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List (Ahmed Hossam).cpp @@ -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; + } + +}; diff --git a/05- May/15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List (Omar Sanad).cpp b/05- May/15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List (Omar Sanad).cpp new file mode 100644 index 000000000..7c7f5715e --- /dev/null +++ b/05- May/15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List (Omar Sanad).cpp @@ -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) {} + * }; + */ diff --git a/05- May/15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List (Osama Ayman).java b/05- May/15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List (Osama Ayman).java new file mode 100644 index 000000000..907cb985b --- /dev/null +++ b/05- May/15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List (Osama Ayman).java @@ -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; + } +} \ No newline at end of file diff --git a/05- May/README.md b/05- May/README.md index 0c09bf734..25bc9decd 100644 --- a/05- May/README.md +++ b/05- May/README.md @@ -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)**


@@ -794,4 +795,52 @@ public: } }; ``` + +
+

+ +## 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; + } + +}; +``` \ No newline at end of file