Linked List Cycle

经典题,送分题,做过无数遍。

public class Solution {
public boolean hasCycle(ListNode head) {
if(head == null) return false;
ListNode slow = head;
ListNode fast = head;
while(fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
if(slow == fast) return true;
}
return false;
}
}


posted @ 2014-12-29 05:37  江南第一少  阅读(88)  评论(0)    收藏  举报