circular linked list
/** * 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) { ListNode quick = head; ListNode slow = head; while (quick != null && quick.next != null) { quick = quick.next.next; slow = slow.next; if (quick == slow) { return true; } } return false; } }