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

141. Linked List Cycle

时间:2017-07-05 21:11:06      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:return   com   lis   cycle   head   fast   null   turn   next   

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

do-while 的应用

public boolean hasCycle(ListNode head) {
        if (head == null) {
            return false;
        }
        ListNode fast = head;
        ListNode slow = head;
        do {
            if (fast == null || fast.next == null) {
                return false;
            }
            fast = fast.next.next;
            slow = slow.next;
        } while (fast != slow);
        if (slow == fast) {
            return true;
        } else {
            return false;
        }
    }

  

141. Linked List Cycle

标签:return   com   lis   cycle   head   fast   null   turn   next   

原文地址:http://www.cnblogs.com/apanda009/p/7123224.html

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