Skip to content

Commit f46c847

Browse files
committed
剑指Offer,链表中环的入口点
1 parent 8198861 commit f46c847

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* 剑指Offer,链表中环的入口点
3+
*/
4+
5+
6+
7+
public class ListNode {
8+
int val;
9+
ListNode next = null;
10+
11+
ListNode(int val) {
12+
this.val = val;
13+
}
14+
}
15+
16+
public class EntryNodeOfListSolution {
17+
18+
public ListNode EntryNodeOfLoop(ListNode pHead)
19+
{
20+
ListNode slow = pHead;
21+
ListNode fast = pHead;
22+
23+
while(fast != null && fast.next != null) {
24+
slow = slow.next;
25+
fast = fast.next.next;
26+
if ( slow == fast ) {
27+
break;
28+
}
29+
}
30+
31+
if (fast == null || fast.next == null) {
32+
return null;
33+
}
34+
35+
fast = pHead;
36+
while(slow != fast) {
37+
slow = slow.next;
38+
fast = fast.next;
39+
}
40+
41+
return slow;
42+
}
43+
}

0 commit comments

Comments
 (0)