/** * Given a linked list, determine if it has a cycle in it. * * Follow up: * Can you solve it without using extra space? * * Tags: Linkedlist, Two pointers */ class LinkedListCycle { public static void main(String[] args) { } /** * Runnner's technique * Check the next and next next of faster node is slower node or not. */ public boolean hasCycle(ListNode head) { if (head == null || head.next == null) return false; ListNode fast = head; ListNode slow = head; while (fast.next != null && fast.next.next != null) { fast = fast.next.next; slow = slow.next; if (fast == slow) return true; } return false; } class ListNode { int val; ListNode next; ListNode(int x) { val = x; next = null; } } }