首页 > 其他 > 详细

Linked List Cycle II

时间:2015-04-09 06:26:27      阅读:220      评论:0      收藏:0      [点我收藏+]

写了无数次,今天终于自己想明白了,但是好像还是记住后明白的

技术分享

plotting tool: http://flowchart.com/

public class Solution {
    public ListNode detectCycle(ListNode head) {
        if(head==null|| head.next==null) return null;
        ListNode run = head, walk = head;
        while(run!=null){
            if(run.next!=null){
                run = run.next;
            }
            run = run.next;
            walk = walk.next;
            if(walk==run) break; // the "here"
        }
        if(run==null) return null; // 注意这里有个判断,可能根本没有循环
        walk = head;
        while(walk!=run){
            walk = walk.next;
            run = run.next;
        }
        return walk;
    }
}

 

Linked List Cycle II

原文:http://www.cnblogs.com/jiajiaxingxing/p/4407774.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!