-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedListCycleII.java
65 lines (58 loc) · 1.56 KB
/
LinkedListCycleII.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
/*
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 LinkedListCycleII {
public static class ListNode {
int val;
ListNode next;
public ListNode(int x) { val = x; }
}
public static ListNode detectCycle(ListNode head) {
if(head== null) return null;
ListNode fast = head;
ListNode slow = head;
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
if(fast == slow) {
fast = head;
while(fast != slow){
fast = fast.next;
slow = slow.next;
}
return fast;
}
}
return null;
}
public static void main(String[] args) {
int n = 6;
ListNode head = new ListNode(1);
ListNode node2 = new ListNode(2);
ListNode node3 = new ListNode(3);
ListNode node4 = new ListNode(4);
ListNode node5 = new ListNode(5);
ListNode node6 = new ListNode(6);
head.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;
node5.next = node6;
node6.next = node3;
System.out.print( "Before: ");
ListNode curr = head;
int test = 20;
while(curr != null && test>=0){
System.out.print( curr.val + " ");
curr = curr.next;
test--;
}
System.out.println();
ListNode res = detectCycle(head);
System.out.println("cycle start node: " + res.val);
return;
}
}