Linked List Cycle II 找链表环入口 @LeetCode
2013-11-09 01:03
483 查看
经典链表环的问题
重写时的注意点写在注解里了
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode detectCycle(ListNode head) {
if(head==null || head.next==null){
return null; // No node or single node ==> No cycle
}
ListNode fast = head;
ListNode slow = head;
while(fast!=null && fast.next!=null && slow!=null){
fast = fast.next.next;
slow = slow.next;
if(fast == slow){
break;
}
}
if(fast != slow){ // no cycle
return null;
}
slow = head;
while(fast!=null && slow!=null){
if(fast == slow){ // Check first! eg: 1,2
break;
}
fast = fast.next;
slow = slow.next;
}
return fast;
}
}
package Level3; import Utility.ListNode; /** * Linked List Cycle II * * Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? * */ public class S130 { public static void main(String[] args) { } public ListNode detectCycle(ListNode head) { if(head == null || head.next ==null){ return null; } ListNode slow = head; ListNode fast = head; // 找到第一次相遇点 while(fast!=null && fast.next!=null && slow!=null){ slow = slow.next; fast = fast.next.next; if(slow == fast){ // 相遇 break; } } // 检查是否因为有环退出或是因为碰到null而退出 if(slow==null || fast==null || fast.next==null){ return null; } // 把慢指针移到链表头,然后保持快慢指针同样的速度移动 // 再次相遇时即为环的入口 slow = head; while(slow != fast){ slow = slow.next; fast = fast.next; } // 现在快慢指针都指向环的入口 return slow; } }
重写时的注意点写在注解里了
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode detectCycle(ListNode head) {
if(head==null || head.next==null){
return null; // No node or single node ==> No cycle
}
ListNode fast = head;
ListNode slow = head;
while(fast!=null && fast.next!=null && slow!=null){
fast = fast.next.next;
slow = slow.next;
if(fast == slow){
break;
}
}
if(fast != slow){ // no cycle
return null;
}
slow = head;
while(fast!=null && slow!=null){
if(fast == slow){ // Check first! eg: 1,2
break;
}
fast = fast.next;
slow = slow.next;
}
return fast;
}
}
相关文章推荐
- Linked List Cycle leetcode II java (寻找链表环的入口)
- 判断链表是否有环及环入口点的求法(Linked List Cycle II )
- Linked List Cycle leetcode II java (寻找链表环的入口)
- 判断链表是否有环及环入口点的求法(Linked List Cycle II )
- 142 Linked List Cycle II(如果链表有环,找到入口结点Medium)
- leetcode-Linked List Cycle II 链表判环以及求环起始位置
- [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环
- Leetcode Linked List Cycle II 循环链表入口
- 求单链表环的入口结点 Linked List Cycle II
- 【题解】【链表】【Leetcode】Linked List Cycle II
- 链表环查找(Linked List Cycle II)
- LeetCode Linked List Cycle II 计算带环的链表的起点
- Linked List Cycle II--找出单向链表中环的起点
- [LeetCode] Linked List Cycle II 链表环起始位置
- 又是一道快慢指针与链表的结合题 Linked List Cycle II
- linked-list-cycle-ii——链表,找出开始循环节点
- Leetcode#142(链表)-Linked List Cycle II
- [Leetcode-142] Linked List Cycle II(链表有环详细分析)
- 链表-leetcode 142 Linked List Cycle II
- Leetcode 链表 Linked List Cycle II