-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay04.java
More file actions
58 lines (57 loc) · 1.63 KB
/
Day04.java
File metadata and controls
58 lines (57 loc) · 1.63 KB
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
package Level1;
public class Day04 {
public static void main(String[] args) {
Day04 tool = new Day04();
ListNode h1 = new ListNode(3);
ListNode h2 = new ListNode(2);
ListNode h3 = new ListNode(0);
ListNode h4 = new ListNode(-4);
ListNode h5 = new ListNode(5);
ListNode h6 = new ListNode(6);
h1.next=h2;
h2.next=h3;
h3.next=h4;
h4.next=h2;
System.out.println(tool.detectCycle(h1).val);
}
public void printList(ListNode head){
ListNode cur = head;
while (cur!=null){
System.out.print(cur.val+"->");
cur=cur.next;
}
System.out.print("null");
}
public ListNode middleNode(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while (fast!=null && fast.next!=null){
fast=fast.next.next;
slow=slow.next;
}
return slow;
}
public ListNode detectCycle(ListNode head) {
if(head==null) return null;
if (head.next==null) return null;
ListNode slow = head;
ListNode fast = head;
while (fast!=null){
slow = slow.next;
if (fast.next!=null){
fast=fast.next.next;
}else {
return null;
}
if (fast==slow){
ListNode ptr = head;
while (ptr!=slow){
ptr=ptr.next;
slow=slow.next;
}
return ptr;
}
}
return null;
}
}