Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
legolas007
CodingInterview
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
CodingInterview
/
Solution23.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
46 lines (44 loc) · 1.82 KB
master
Breadcrumbs
CodingInterview
/
Solution23.java
Copy path
Top
File metadata and controls
Code
Blame
46 lines (44 loc) · 1.82 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package com.usher.algorithm.offer;
/**
* @Author: Usher
* @Description:
* 链表中环的入口节点
* 使用双指针,一个指针 fast 每次移动两个节点,
* 一个指针 slow 每次移动一个节点。因为存在环,
* 所以两个指针必定相遇在环中的某个节点上。
* 此时 fast 移动的节点数为 x+2y+z,slow 为 x+y,
* 由于 fast 速度比 slow 快一倍,
* 因此 x+2y+z=2(x+y),得到 x=z。
在相遇点,slow 要到环的入口点还需要移动 z 个节点,
如果让 fast 重新从头开始移动,并且速度变为每次移动一个节点,
那么它到环入口点还需要移动 x 个节点。在上面已经推导出 x=z,
因此 fast 和 slow 将在环入口点相遇。
*/
public class Solution23 {
/**
第一步,找环中相汇点。分别用p1,p2指向链表头部,p1每次走一步,p2每次走二步,直到p1==p2找到在环中的相汇点。
第二步,找环的入口。接上步,当p1==p2时,p2所经过节点数为2x,p1所经过节点数为x,
设环中有n个节点,p2比p1多走一圈有2x=n+x; n=x;可以看出p1实际走了一个环的步数,
再让p2指向链表头部,p1位置不变,p1,p2每次走一步直到p1==p2; 此时p1指向环的入口。
* @param pHead
* @return
*/
public ListNode EntryNodeOfLoop(ListNode pHead) {
if (pHead == null)
return null;
ListNode slow = pHead,fast = pHead;
while (fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
if(slow == fast){
fast = pHead;
while (slow != fast){
slow = slow.next;
fast = fast.next;
}
return slow;
}
}
return null;
}
}
You can’t perform that action at this time.