码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode141

时间:2017-04-22 10:31:49      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:nod   title   lin   list   false   com   while   problem   ems   

/**
 * 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

标签:nod   title   lin   list   false   com   while   problem   ems   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!