100. 检测链表中是否有环
分享给更多人
容易 -通过 / -执行
单向链表 就是朝一个方向串联在一起的节点。
class Node { val: any next: Node constructor(val: any, next:Node) { this.val = val this.next = next }}const node2 = new Node(2)const node1 = new Node(1, node2) // connect 1 -> 2
一个节点如果链接到了在它之前的节点,就形成了一个环。
请写一个函数来检测单向链表中是否有环。
你的方法的时间空间复杂度是多少?能写一个空间复杂度是O(1)的解法吗?