Skip to content

Latest commit

 

History

History
21 lines (17 loc) · 433 Bytes

Code141.md

File metadata and controls

21 lines (17 loc) · 433 Bytes

Linked List Cycle

public class Solution {
    public boolean hasCycle(ListNode head) {
        if (head == null) {
            return false;
        }

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

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

        return fast == slow;
    }
}