Skip to content

Commit 8dac0b4

Browse files
♻️ Day 27
1 parent 6ecd95f commit 8dac0b4

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
3. [Bag of Tokens](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/562/week-4-october-22nd-october-28th/3506/) ➡️ [CPP Solution](Week4/bagOfTokensScore.cpp)
4343
4. [Stone Game IV](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/562/week-4-october-22nd-october-28th/3507/) ➡️ [CPP Solution](Week4/winnerSquareGame.cpp)
4444
5. [Champagne Tower](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/562/week-4-october-22nd-october-28th/3508/) ➡️ [CPP Solution](Week4/champagneTower.cpp)
45+
6. [Linked List Cycle II](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/562/week-4-october-22nd-october-28th/3509/) ➡️ [CPP Solution](Week4/detectCycle.cpp)
4546

4647
## Week 5 🚧
4748

Week4/detectCycle.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
ListNode *detectCycle(ListNode *head) {
12+
if(head == NULL) return NULL;
13+
14+
ListNode* slow = head;
15+
ListNode* fast = head;
16+
17+
while(fast != NULL && fast->next != NULL) {
18+
slow = slow->next;
19+
fast = fast->next->next;
20+
21+
if(slow == fast) {
22+
slow = head;
23+
while(slow != fast) {
24+
slow = slow->next;
25+
fast = fast->next;
26+
}
27+
return slow;
28+
}
29+
}
30+
31+
return NULL;
32+
}
33+
};

0 commit comments

Comments
 (0)