-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC_142_LinkedListCycleII.java
More file actions
43 lines (36 loc) · 930 Bytes
/
LC_142_LinkedListCycleII.java
File metadata and controls
43 lines (36 loc) · 930 Bytes
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
/**
* LC_142_LinkedListCycleII
*/
public class LC_142_LinkedListCycleII {
// ListNode Class
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
public ListNode detectCycle(ListNode head) {
if(head == null || head.next == null){
return null;
}
ListNode oneX,twoX;
oneX = twoX = head;
while(twoX != null && twoX.next != null){
// Update
twoX = twoX.next.next;
oneX = oneX.next;
if(oneX == twoX){
oneX = head;
while(oneX != twoX){
oneX = oneX.next;
twoX = twoX.next;
}
return oneX;
}
}
return null;
}
// Nodes are being Passed by LeetCode itsef in main
}