[LintCode] Linked List Cycle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public boolean hasCycle(ListNode head) { // write your code here if(head == null) return false; ListNode fast = head; ListNode slow = head; while(fast.next != null && fast.next.next != null) { slow = slow.next; fast = fast.next.next; if(slow == fast) return true; } return false; } |
Leave A Comment