Community

Start a Thread


Notifications
Subscribe You’re not receiving notifications from this thread.

Detect Loop in Linked List (Main Thread)

Here is the interview question prompt, presented for reference.

What is the best way to find out if a linked list contains a loop? You're given the following data structure as the definition of a linked list node:

class LinkedListNode {
    constructor(val) {
        this.val = val;
        this.next = null;
    }
}

A loop or a cycle in graph theory is a path of nodes and edges where a node is reachable from itself.

Implement a detectLoop method that takes a linked list head node as the parameter and returns true or false depending on whether there's a cycle.

Constraints

  • Length of the linked list <= 10000
  • Value stored in each node will be between -1000000000 and 1000000000
  • Expected time complexity : O(n)
  • Expected space complexity : O(1)

You can see the full challenge with visuals at this link.

Challenges • Asked almost 7 years ago by Jake from AlgoDaily

Jake from AlgoDaily Commented on Nov 30, 2017:

This is the main discussion thread generated for Detect Loop in Linked List.