首页 > 其他 > 详细

leetcode141

时间:2017-04-22 10:31:56      阅读:148      评论:0      收藏:0      [点我收藏+]
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution
    {
        public bool HasCycle(ListNode head)
        {
            //if (head == null)
            //{
            //    return false;
            //}
            //else
            //{
            //    var temp = head;
            //    while (head.next != null)
            //    {
            //        var cur = head.next;
            //        if (temp == cur)
            //        {
            //            return true;
            //        }
            //        else
            //        {
            //            head = head.next;
            //        }
            //    }
            //    return false;
            //}

            if (head == null) return false;
            ListNode walker = head;
            ListNode runner = head;
            while (runner.next != null && runner.next.next != null)
            {
                walker = walker.next;
                runner = runner.next.next;
                if (walker == runner) return true;
            }
            return false;
        }
    }

https://leetcode.com/problems/linked-list-cycle/#/description

leetcode141

原文:http://www.cnblogs.com/asenyang/p/6747011.html

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