detect circle in linked list
2022年5月1日小于 1 分钟
detect circle in linked list
Question
A Singly Linked List is a bunch of nodes linked in one direction.
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
A Node might link to a node before it, thus creating a circle.
Can you write a function to detect it?
Follow-up
What is the space cost for your approach? Can you solve it without extra space?
Code
/**
* @param {Node} head
* @return {boolean}
*/
function hasCircle(head) {
// your code here
}