Skip to content

Commit 5e8abb2

Browse files
✨ Day 7; Week 1 ✔️
1 parent baf3f6a commit 5e8abb2

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
55
[Challenge Link](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/)
66

7-
## Week 1 🚧
7+
## Week 1
88

99
1. [Number of Recent Calls](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/559/week-1-october-1st-october-7th/3480/) ➡️ [CPP Solution](Week1/RecentCounter.cpp)
1010
2. [Combination Sum](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/559/week-1-october-1st-october-7th/3481/) ➡️ [CPP Solution](Week1/combinationSum.cpp)
1111
3. [K-diff Pairs in an Array](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/559/week-1-october-1st-october-7th/3482/) ➡️ [CPP Solution](Week1/findPairs.cpp)
1212
4. [Remove Covered Intervals](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/559/week-1-october-1st-october-7th/3483/) ➡️ [CPP Solution](Week1/removeCoveredIntervals.cpp)
1313
5. [Complement of Base 10 Integer](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/559/week-1-october-1st-october-7th/3484/) ➡️ [CPP Solution](Week1/bitwiseComplement.cpp)
1414
6. [Insert into a Binary Search Tree](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/559/week-1-october-1st-october-7th/3485/) ➡️ [CPP Solution](Week1/insertIntoBST.cpp)
15+
7. [Rotate List](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/559/week-1-october-1st-october-7th/3486/) ➡️ [CPP Solution](Week1/rotateRight.cpp)
1516

1617
## Week 2 🚧
1718

Week1/rotateRight.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode* rotateRight(ListNode* head, int k) {
14+
ListNode* iter = head, *prev, *iter2;
15+
int numNodes = 0;
16+
17+
if(k == 0 || !head) return head;
18+
19+
while(iter) {
20+
iter = iter->next;
21+
numNodes++;
22+
}
23+
24+
k %= numNodes;
25+
26+
if(numNodes == 1 || k == 0) return head;
27+
28+
iter = head;
29+
30+
while(numNodes - k > 0) {
31+
prev = iter;
32+
iter = iter->next;
33+
numNodes--;
34+
}
35+
36+
prev->next = NULL;
37+
38+
iter2 = iter;
39+
40+
while(iter2->next) iter2 = iter2->next;
41+
42+
iter2->next = head;
43+
head = iter;
44+
45+
return head;
46+
}
47+
};

0 commit comments

Comments
 (0)