-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEntryNodeOfLoop.java
More file actions
41 lines (33 loc) · 900 Bytes
/
EntryNodeOfLoop.java
File metadata and controls
41 lines (33 loc) · 900 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
package com.company;
import org.junit.Test;
public class EntryNodeOfLoop {
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
public ListNode EntryNodeOfLoop(ListNode pHead) {
if (pHead.next == null || pHead.next.next == null)
return null;
ListNode slow = pHead.next;
ListNode fast = pHead.next.next;
while (fast != null) {
if (fast == slow) {
fast = pHead;
while (fast != slow) {
fast = fast.next;
slow = slow.next;
}
return fast;
}
slow = slow.next;
fast = fast.next.next;
}
return null;
}
@Test
public void test() {
}
}