Skip to content

Latest commit

 

History

History
27 lines (26 loc) · 483 Bytes

linked-list-cycle.md

File metadata and controls

27 lines (26 loc) · 483 Bytes
/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} head
 * @return {boolean}
 */
var hasCycle = function(head) {
    let fast, slow;
    fast = head;
    slow = head;
    while(fast !== null && fast.next !== null) {
        fast = fast.next.next;
        slow = slow.next;
        if (fast === slow) {
            return true;
        }
    }
    return false;
};