Leetcode 141 Linked List Cycle

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
}
posted @ 2015-06-19 22:43  lilixu  阅读(112)  评论(0编辑  收藏  举报