xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

LeetCode & list cycle

链表是否存在环检测

singly-linked list

单链表

"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2020-11-17
 * @modified
 *
 * @description 链表
 * @difficulty Easy Medium Hard
 * @complexity O(n)
 * @augments
 * @example
 * @link
 * @solutions
 *
 * @best_solutions
 *
 */

const log = console.log;

// 节点
function ListNode(val, next) {
  this.val = 0 || val;
  this.next = null || next;
}

// 链表
function LinkedList(value) {
  const node = new ListNode(value, ``);
  if(!head) {
    head = node;
  } else {
    let current = head;
    while(current.next) {
      current = current.next;
    }
    current.next = node;
  }
};


two-pointers

双指针 / 快慢指针

Object 循环引用错误

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *   this.val = val;
 *   this.next = null;
 * }
 */

/**
 * @param {ListNode} head
 * @return {boolean}
 */
var hasCycle = function(head) {
  let result = false;
  try {
    JSON.stringify(head);
    // result = false;
  } catch(err) {
    result = true;
  }
  return result;
};

refs

https://leetcode.com/problemset/all/?topicSlugs=two-pointers

https://leetcode.com/problems/linked-list-cycle


Flag Counter

©xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


原文地址:https://www.cnblogs.com/xgqfrms/p/13521700.html