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

LeetCode & list cycle

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



©xgqfrms 2012-2020

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


posted @ 2020-08-18 09:48  xgqfrms  阅读(129)  评论(2编辑  收藏  举报