Linked List Cycle

public class Solution {
    public boolean hasCycle(ListNode head) {
        if(head==null || head.next==null) return false;
        ListNode walk = head, run = head;
        while(run!=null){
            if(run.next!=null){
                run = run.next;
            } 
            run = run.next;
            walk = walk.next;
            if(run==walk) return true;
        }
        return false;
    }
}

 

posted @ 2015-04-09 04:33  世界到处都是小星星  阅读(79)  评论(0编辑  收藏  举报