From 16cf9925e38899f32f9cb5ff6327eaad414ff58f Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Sat, 6 May 2023 00:35:54 +0000 Subject: [PATCH 01/18] Add new daily problem --- .github/data/problems.json | 4 ++++ .../.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/06- Number of Subsequences That Satisfy the Given Sum Condition/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 814f8c3e9..a9dfea935 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -507,6 +507,10 @@ { "day": "05", "title": "Maximum Number of Vowels in a Substring of Given Length" + }, + { + "day": "06", + "title": "Number of Subsequences That Satisfy the Given Sum Condition" } ] } \ No newline at end of file diff --git a/05- May/06- Number of Subsequences That Satisfy the Given Sum Condition/.gitkeep b/05- May/06- Number of Subsequences That Satisfy the Given Sum Condition/.gitkeep new file mode 100644 index 000000000..e69de29bb From b115fe4e19434db9949701f6c90a9352308e1784 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Sun, 7 May 2023 00:39:28 +0000 Subject: [PATCH 02/18] Add new daily problem --- .github/data/problems.json | 4 ++++ .../.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/07- Find the Longest Valid Obstacle Course at Each Position/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index a9dfea935..d1ddcbc65 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -511,6 +511,10 @@ { "day": "06", "title": "Number of Subsequences That Satisfy the Given Sum Condition" + }, + { + "day": "07", + "title": "Find the Longest Valid Obstacle Course at Each Position" } ] } \ No newline at end of file diff --git a/05- May/07- Find the Longest Valid Obstacle Course at Each Position/.gitkeep b/05- May/07- Find the Longest Valid Obstacle Course at Each Position/.gitkeep new file mode 100644 index 000000000..e69de29bb From 4ec5e304dae73221ee267ff97f9b55c29cab43e2 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Mon, 8 May 2023 00:25:11 +0000 Subject: [PATCH 03/18] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/08- Matrix Diagonal Sum/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/08- Matrix Diagonal Sum/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index d1ddcbc65..cfcdb5ad9 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -515,6 +515,10 @@ { "day": "07", "title": "Find the Longest Valid Obstacle Course at Each Position" + }, + { + "day": "08", + "title": "Matrix Diagonal Sum" } ] } \ No newline at end of file diff --git a/05- May/08- Matrix Diagonal Sum/.gitkeep b/05- May/08- Matrix Diagonal Sum/.gitkeep new file mode 100644 index 000000000..e69de29bb From f01cfbb2cbd03cd0b7393105028f124ca05f4c2e Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Tue, 9 May 2023 00:48:47 +0000 Subject: [PATCH 04/18] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/09- Spiral Matrix/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/09- Spiral Matrix/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index cfcdb5ad9..30ac76bbb 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -519,6 +519,10 @@ { "day": "08", "title": "Matrix Diagonal Sum" + }, + { + "day": "09", + "title": "Spiral Matrix" } ] } \ No newline at end of file diff --git a/05- May/09- Spiral Matrix/.gitkeep b/05- May/09- Spiral Matrix/.gitkeep new file mode 100644 index 000000000..e69de29bb From 5b302ad85e82141bb74d3ea35040c6941c9fda9b Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Wed, 10 May 2023 00:32:38 +0000 Subject: [PATCH 05/18] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/10- Spiral Matrix II/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/10- Spiral Matrix II/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 30ac76bbb..c72160be9 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -523,6 +523,10 @@ { "day": "09", "title": "Spiral Matrix" + }, + { + "day": "10", + "title": "Spiral Matrix II" } ] } \ No newline at end of file diff --git a/05- May/10- Spiral Matrix II/.gitkeep b/05- May/10- Spiral Matrix II/.gitkeep new file mode 100644 index 000000000..e69de29bb From b88fe3669c8534aaa465bde25fa6f12155737948 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Thu, 11 May 2023 00:42:08 +0000 Subject: [PATCH 06/18] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/11- Uncrossed Lines/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/11- Uncrossed Lines/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index c72160be9..3f3b0c8fc 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -527,6 +527,10 @@ { "day": "10", "title": "Spiral Matrix II" + }, + { + "day": "11", + "title": "Uncrossed Lines" } ] } \ No newline at end of file diff --git a/05- May/11- Uncrossed Lines/.gitkeep b/05- May/11- Uncrossed Lines/.gitkeep new file mode 100644 index 000000000..e69de29bb From 11e911526b9a8210edb19da8695bec434c693603 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Fri, 12 May 2023 00:45:32 +0000 Subject: [PATCH 07/18] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/12- Solving Questions With Brainpower/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/12- Solving Questions With Brainpower/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 3f3b0c8fc..e90d3d656 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -531,6 +531,10 @@ { "day": "11", "title": "Uncrossed Lines" + }, + { + "day": "12", + "title": "Solving Questions With Brainpower" } ] } \ No newline at end of file diff --git a/05- May/12- Solving Questions With Brainpower/.gitkeep b/05- May/12- Solving Questions With Brainpower/.gitkeep new file mode 100644 index 000000000..e69de29bb From 47807ede272a2087263e50165f22784f888f71c5 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Sat, 13 May 2023 00:00:05 +0000 Subject: [PATCH 08/18] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/13- Count Ways To Build Good Strings/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/13- Count Ways To Build Good Strings/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index e90d3d656..ccc399845 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -535,6 +535,10 @@ { "day": "12", "title": "Solving Questions With Brainpower" + }, + { + "day": "13", + "title": "Count Ways To Build Good Strings" } ] } \ No newline at end of file diff --git a/05- May/13- Count Ways To Build Good Strings/.gitkeep b/05- May/13- Count Ways To Build Good Strings/.gitkeep new file mode 100644 index 000000000..e69de29bb From ad7d52e1fe91b50d27c22e778b4f0ee3b8c6259f Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Sat, 13 May 2023 01:22:17 +0000 Subject: [PATCH 09/18] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/13- Count Ways To Build Good Strings/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/13- Count Ways To Build Good Strings/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index e90d3d656..ccc399845 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -535,6 +535,10 @@ { "day": "12", "title": "Solving Questions With Brainpower" + }, + { + "day": "13", + "title": "Count Ways To Build Good Strings" } ] } \ No newline at end of file diff --git a/05- May/13- Count Ways To Build Good Strings/.gitkeep b/05- May/13- Count Ways To Build Good Strings/.gitkeep new file mode 100644 index 000000000..e69de29bb From e33a6d93d067dd7628056fa2b682d80e6b64f931 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Sun, 14 May 2023 00:27:04 +0000 Subject: [PATCH 10/18] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/14- Maximize Score After N Operations/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/14- Maximize Score After N Operations/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index ccc399845..0986d48cb 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -539,6 +539,10 @@ { "day": "13", "title": "Count Ways To Build Good Strings" + }, + { + "day": "14", + "title": "Maximize Score After N Operations" } ] } \ No newline at end of file diff --git a/05- May/14- Maximize Score After N Operations/.gitkeep b/05- May/14- Maximize Score After N Operations/.gitkeep new file mode 100644 index 000000000..e69de29bb From c59bd4c560844c7646eeab273534fe2d6ae0613c Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Sun, 14 May 2023 01:41:13 +0000 Subject: [PATCH 11/18] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/14- Maximize Score After N Operations/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/14- Maximize Score After N Operations/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index ccc399845..0986d48cb 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -539,6 +539,10 @@ { "day": "13", "title": "Count Ways To Build Good Strings" + }, + { + "day": "14", + "title": "Maximize Score After N Operations" } ] } \ No newline at end of file diff --git a/05- May/14- Maximize Score After N Operations/.gitkeep b/05- May/14- Maximize Score After N Operations/.gitkeep new file mode 100644 index 000000000..e69de29bb From 981fbc28dbceda536f59e93f3a01440e8d9fbef0 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Mon, 15 May 2023 00:29:45 +0000 Subject: [PATCH 12/18] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/15- Swapping Nodes in a Linked List/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/15- Swapping Nodes in a Linked List/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 0986d48cb..52af9e62f 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -543,6 +543,10 @@ { "day": "14", "title": "Maximize Score After N Operations" + }, + { + "day": "15", + "title": "Swapping Nodes in a Linked List" } ] } \ No newline at end of file diff --git a/05- May/15- Swapping Nodes in a Linked List/.gitkeep b/05- May/15- Swapping Nodes in a Linked List/.gitkeep new file mode 100644 index 000000000..e69de29bb From dc63534a686558c78d7ab9115a0a09311a6c90b9 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Mon, 15 May 2023 01:39:32 +0000 Subject: [PATCH 13/18] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/15- Swapping Nodes in a Linked List/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/15- Swapping Nodes in a Linked List/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 0986d48cb..52af9e62f 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -543,6 +543,10 @@ { "day": "14", "title": "Maximize Score After N Operations" + }, + { + "day": "15", + "title": "Swapping Nodes in a Linked List" } ] } \ No newline at end of file diff --git a/05- May/15- Swapping Nodes in a Linked List/.gitkeep b/05- May/15- Swapping Nodes in a Linked List/.gitkeep new file mode 100644 index 000000000..e69de29bb From 3793f3114f72249588f6eae70d4d7e75bfb6b3bc Mon Sep 17 00:00:00 2001 From: Ibrahim Khaild <103538501+Hima-khalid@users.noreply.github.com> Date: Mon, 15 May 2023 11:30:39 +0300 Subject: [PATCH 14/18] Create 15- Swapping Nodes in a Linked List ( Ibrahim Khalid ).cpp --- ...es in a Linked List ( Ibrahim Khalid ).cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 05- May/15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List ( Ibrahim Khalid ).cpp 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; + } +}; From 037b306e46d3d3ac31a5958bcf43a3534a28d569 Mon Sep 17 00:00:00 2001 From: Omar Sanad <108091921+OmarSanad3@users.noreply.github.com> Date: Mon, 15 May 2023 13:00:28 +0300 Subject: [PATCH 15/18] Added Sanad's Solution, May 15th --- ...ng Nodes in a Linked List (Omar Sanad).cpp | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 05- May/15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List (Omar Sanad).cpp 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) {} + * }; + */ From 8e42370ba8bcc451c45d3e043c4dcf66b10b5d6b Mon Sep 17 00:00:00 2001 From: Osama Ayman Date: Mon, 15 May 2023 16:01:23 +0300 Subject: [PATCH 16/18] Added my sol to 15 May --- ... Nodes in a Linked List (Osama Ayman).java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 05- May/15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List (Osama Ayman).java 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 From de6fd481e26cdac4564dc03f4402e404c2802ef3 Mon Sep 17 00:00:00 2001 From: Ahmed Hossam <63050133+7oSkaaa@users.noreply.github.com> Date: Mon, 15 May 2023 17:39:30 +0300 Subject: [PATCH 17/18] Create 15- Swapping Nodes in a Linked List (Ahmed Hossam).cpp --- ... Nodes in a Linked List (Ahmed Hossam).cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 05- May/15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List (Ahmed Hossam).cpp 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; + } + +}; From b63fe851b6dcce05a6b231412ffbbcfb6a575aee Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Mon, 15 May 2023 14:40:24 +0000 Subject: [PATCH 18/18] Add new daily problem to README --- 05- May/README.md | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) 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