Check if a linked list is Circular Linked List | Link list Node ; * This function returns true if given linked list is circular , else false . ; An empty linked list is circular ; Next of head ; This loop would stop in both cases ( 1 ) If Circular ( 2 ) Not circular ; If loop stopped because of circular condition ; Utility function to create a new node . ; Start with the empty list ; Making linked list circular
class Node { constructor ( val ) { this . data = val ; this . next = null ; } } function isCircular ( head ) { if ( head == null ) return true ; node = head . next ; while ( node != null && node != head ) node = node . next ; return ( node == head ) ; } function newNode ( data ) { temp = new Node ( ) ; temp . data = data ; temp . next = null ; return temp ; } head = newNode ( 1 ) ; head . next = newNode ( 2 ) ; head . next . next = newNode ( 3 ) ; head . next . next . next = newNode ( 4 ) ; document . write ( isCircular ( head ) ? " " : " " ) ; head . next . next . next . next = head ; document . write ( isCircular ( head ) ? " " : " " ) ;