-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution142.java
67 lines (60 loc) · 1.66 KB
/
Solution142.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
//
// Note: Do not modify the linked list.
//
// Follow up:
// Can you solve it without using extra space?
public class Solution142 {
static class ListNode {
int val;
ListNode next;
ListNode(int x, ListNode n) {
val = x;
next = n;
}
}
public ListNode detectCycle(ListNode head) {
ListNode p1 = head, p2 = head;
while (p1 != null && p2 != null) {
p1 = p1.next;
p2 = p2.next;
if (p1 != null) {
p1 = p1.next;
}
if (p1 == p2) {
break;
}
}
// Does not have cycle
if (p1 == null) {
return null;
}
p2 = head;
while (p1 != p2) {
p1 = p1.next;
p2 = p2.next;
}
return p1;
}
public static void main(String[] args) {
Solution142 s = new Solution142();
ListNode cycle = new ListNode(5, null);
System.out.println(s.detectCycle(cycle));
ListNode a = new ListNode(1, null);
ListNode b = new ListNode(2, null);
ListNode c = new ListNode(3, null);
ListNode d = new ListNode(4, null);
ListNode e = new ListNode(5, null);
ListNode f = new ListNode(6, null);
ListNode g = new ListNode(7, null);
a.next = b;
b.next = c;
c.next = d;
d.next = e;
e.next = f;
f.next = g;
g.next = d;
System.out.println(s.detectCycle(a).val);
System.out.println(s.detectCycle(null));
}
}