We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f6719eb commit 014d22bCopy full SHA for 014d22b
Linked_List_Cycle_II.cc
@@ -0,0 +1,29 @@
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
+ // IMPORTANT: Please reset any member data you declared, as
13
+ // the same Solution instance will be reused for each test case.
14
+ ListNode *fast = head, *slow = head;
15
+ while (fast && fast->next) {
16
+ fast = fast->next->next;
17
+ slow = slow->next;
18
+ if (fast == slow) {
19
+ fast = head;
20
+ while (fast != slow) {
21
+ fast = fast->next;
22
23
+ }
24
+ return fast;
25
26
27
+ return NULL;
28
29
+};
0 commit comments