美文网首页
链表是否成环

链表是否成环

作者: Joah_l | 来源:发表于2019-11-21 11:08 被阅读0次
// NOTE: 判断链表是否成环
function linkIsCycle(head) {
  if (head === null) {
    return false
  }
  if (head.next === null) {
    return false
  }
  let fast = head.next
  let slow = head
  while (fast && fast.next) {
    if (fast === slow) return true;
    slow = slow.next;
    const next = fast.next;
    fast = next && next.next;
  }
  return false;
}

相关文章

网友评论

      本文标题:链表是否成环

      本文链接:https://www.haomeiwen.com/subject/dmizictx.html