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

Leetcode 141 Linked List Cycle

时间:2015-06-19 23:00:46      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

Given a linked list, determine if it has a cycle in it.

Follow up:
Can you solve it without using extra space?

LL经典题:一个指针每次前进1node,另一个指针每次前进2node,如果有环则必会到达同一node。

var hasCycle = function(head) {
    if(!head) 
        return false
    var a = head
    var b = head
    while(b.next && b.next.next){
        b = b.next.next
        a = a.next
        if(a===b)
            return true
    }
    return false
}

Leetcode 141 Linked List Cycle

标签:

原文地址:http://www.cnblogs.com/lilixu/p/4589867.html

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