Check if a linked list is circular

Determine if a given singly linked list is circular, where a circular list's final node points back to some other node in the list.

Hint

To detect a cycle in a linked list, employ a 'slow' and a 'fast' pointer. If at any point during the traversal, the slow pointer encounters the fast pointer before the end of the loop, it confirms the presence of a cycle within the list. Initially, the slow pointer is assigned to the head of the list, and the fast pointer is assigned to the next node of the head

# Python implementation
class Node:
  def __init__(self, data):
    self.data = data
    self.next = None

class LL:
  def __init__(self, nodes):
    self.head = nodes[0]

    tail = self.head
    for i in range(1, len(nodes)):
      tail.next = nodes[i]
      tail = nodes[i]

  def isCircular(self):
    if not self.head:
      return False
    
    slow = self.head
    fast = self.head.next
    while fast and fast.next:
      if slow == fast:
        return True
      
      slow = slow.next
      fast = fast.next.next

    return False

  '''
  def print(self):
    tail = self.head
    while tail:
      print(tail.data, end = " ")
      tail = tail.next
    
    print()
  '''

n1 = Node(1)
n2 = Node(2)
n3 = Node(3)

ll = LL([n1, n2, n3])

print(ll.isCircular())
// Javascript implementation
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

class LL {
  constructor(nodes) {
    this.head = nodes[0];

    let tail = this.head;
    for (let i = 1; i < nodes.length; i++) {
      tail.next = nodes[i];
      tail = nodes[i];
    }
  }

  isCircular() {
    if (!this.head) {
      return false;
    }

    let slow = this.head;
    let fast = this.head.next;

    while (fast && fast.next) {
      if (slow == fast) {
        return true;
      }
      slow = slow.next;
      fast = fast.next.next;
    }

    return false;
  }
}

const n1 = new Node(1);
const n2 = new Node(2);
const n3 = new Node(3);

const ll = new LL([n1, n2, n3]);

console.log(ll.isCircular())