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 发布文章使用:只允许注册用户才可以访问!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/13521700.html
未经授权禁止转载,违者必究!