Linked List Cycle

Given a linked list, determine if it has a cycle in it.

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

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
        if(head == null)
            return false;
        
        ListNode dummy = new ListNode(0);
        
        dummy.next = head;
        
        ListNode fast = dummy, slow = dummy;
        
        while(fast.next != null &&fast.next.next != null){
            fast = fast.next.next;
            slow = slow.next;
            
            if(slow == fast){
                return true;
            }
        }
        
        return false;
    }
}

Last updated