美文网首页LeetCode solutions
142. Linked List Cycle II

142. Linked List Cycle II

作者: 番茄晓蛋 | 来源:发表于2017-06-10 10:51 被阅读1次

    **Description**Hints**Submissions**Solutions

    Total Accepted: 112637
    Total Submissions: 363331
    Difficulty: Medium
    Contributor: LeetCode

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null
    .
    Note: Do not modify the linked list.
    Follow up:Can you solve it without using extra space?

    Hide Tags
    Linked List Two Pointers
    Hide Similar Problems
    (E) Linked List Cycle (M) Find the Duplicate Number

    ** 解题思路 **
    用快慢指针,快指针一次走两步,慢指针一次走一步,来先确定是否有cycle。
    如果存在cycle,则将fast = head,再重新 fast =fast.next, slow = slow.next 两个指针都单步走,找到fast = slow点即可确定cycle起始点。

    /**
     * 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;
            
            ListNode slow = head;
            ListNode fast = head;
            boolean isCycle = false;
            
            while (fast != null && fast.next != null) {
                fast = fast.next.next;
                slow = slow.next;
                if (fast == slow) {
                    isCycle = true;
                    break; // don't forget to break the loop
                }
            }
            if (!isCycle) return null;
            
            // find the node while cycle begins
            fast = head;
            while (fast != slow) {
                fast = fast.next;
                slow = slow.next;
            }
            
            return slow;
        }
    }
    

    相关文章

      网友评论

        本文标题:142. Linked List Cycle II

        本文链接:https://www.haomeiwen.com/subject/vpmqqxtx.html