-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList cycle.java
53 lines (50 loc) · 1.43 KB
/
LinkedList cycle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
////1111111111111
public class Solution {
public boolean hasCycle(ListNode head) {
if(head == null || head.next == null) return false;
ListNode slow = head;
ListNode fast = head;
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
if(slow == fast) return true;
}
return false;
}
}
// we have two pointers called slow and fast
// if there is a cycle in the linkedlist, slow will meet fast(no cycle return null)
// fast's speed is double as slow so the total length will double as well
// 2(a + b) = 2b + a + c;
// we know a = c
public class Solution {
public ListNode detectCycle(ListNode head) {
if(head == null || head.next == null) return null;
ListNode slow = head;
ListNode fast = head;
while(true){
if(fast == null || fast.next == null) return null; // no cycle in the linked list
slow = slow.next;
fast = fast.next.next;
if(slow == fast) break;
}
// 2(a+b) = a + c + 2b;
slow = head;
while(slow != fast){
slow = slow.next;
fast = fast.next;
}
return slow;
}
}