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?

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

        ListNode slow = head;
        ListNode fast = head.next;

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

        while(head != slow.next) {
            head = head.next;
            slow = slow.next;
        }
        return head;
    }
}



second time practice
public class Solution {
    public ListNode detectCycle(ListNode head) {
        if(head == null || head.next == null) return null;

        ListNode slow = head;
        ListNode fast = head.next;

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

        ListNode head2 = slow.next;
        while(head != head2) {
            head = head.next;
            head2 = head2.next;    
        }
        return head;
    }
}

results matching ""

    No results matching ""