标签:his turn auto ext off == string slow str
/*
链表中环的入口结点
*/
//思路,使用两个指针,一个快,一个慢,如果快的指针与慢的指针同时直到同一个节点,那么存在环。
public class Class21 {
//复杂问题分解成为几个简单问题(本题分为三步:找出环中任一结点;得到环的个数;找到入口结点)
static class ListNode{
int val;
ListNode next = null;
ListNode(int val){
this.val = val;
}
}
public ListNode findCircleNode(ListNode head){
if(head == null){
return null;
}
ListNode Slow = head;
ListNode Fast = head;
while(Fast != null){
Fast = Fast.next;
Slow = Slow.next;
if(Fast != null){
Fast = Fast.next;
}
if((Slow != null) && (Slow == Fast){
return Slow;
}
}
return null;
}
public ListNode theDoorOfCircle(ListNode head){
ListNode findCircleNode = findCircleNode(head);
if(findCircleNode == null){
return null;
}
int numOfCircle = 1;
ListNode newNode = findCircleNode.next;
while(newNode != findCircleNode){
numOfCircle++;
newNode = newNode.next;
}
newNode = head;
for(int i = 0; i < numOfCircle; i++){
newNode = newNode.next;
}
ListNode newNode2 = head;
while(newNode != newNode2){
newNode = newNode.next;
newNode2 = newNode.next;
}
return newNode;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
标签:his turn auto ext off == string slow str
原文地址:https://www.cnblogs.com/zhuozige/p/12463401.html