Tianhe Gao

LC 141. Linked List Cycle

 1/**
 2 * Definition for singly-linked list.
 3 * function ListNode(val) {
 4 *     this.val = val;
 5 *     this.next = null;
 6 * }
 7 */
 8
 9/**
10 * @param {ListNode} head
11 * @return {boolean}
12 */
13var hasCycle = function (head) {
14  if (head === null) return false
15  let slow = head, fast = head.next
16  while (fast && fast.next) {
17    if (slow.next === fast.next.next) return true
18    slow = slow.next
19    fast =  fast.next.next
20  }
21  return false
22};

No notes link to this note

Welcome to tell me your thoughts via "email"
UP