142 Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.

Note:Do not modify the linked list.

Follow up: Can you solve it without using extra space?

同上题一样,花空间的要用hashset。不用的话,就双指针。解法竟然跟287 Find the Duplicate number 一样

public ListNode detectCycle(ListNode head) {  
    if (head == null || head.next == null) {
        return null;
    }

    ListNode fast = head;
    ListNode slow = head;

    while (fast != null && fast.next != null) {
        fast = fast.next.next;
        slow = slow.next;
        if (fast == slow) {
            break;
        }
    }

    if (fast == null || fast.next == null) {
        return null;
    }

    slow = head;

    while (slow != fast) {
        fast = fast.next;
        slow = slow.next;
    }

    return slow;
}

Last updated