题目描述
解题思路先找到第一次相遇的地方,再将另一指针置为head,两指针再以相同的速度前进,第一次遇见的地方就是循环链开始的节点位置。
证明如下
代码如下
ListNode *delectCycle(ListNode *head){
if(head == null || head -> next == null){
return null;
}
ListNode * firstp = head;
ListNode * secondp = head;
boolean isCycle = false;
while(firstp != null && secondp != null){
firstp = firstp -> next;
if(secondp -> next == null){
return null;
}
secondp = secondp -> next -> next;
if(firstp == secondp){
isCycle = ture;
break;
}
}
if(!isCycle){
return null;
}
firstp = head;
while(firstp != secondp){
firstp = firstp -> next;
secondp = secondp -> next;
}
return firstp;
}