100. detect circle in linked list

JavaScriptAlgorithm
Is this helpful ?

easy  - accepted / - tried

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?

Let's try to solve this problem within 10 mintues.

Discuss(25)