Sum of the nodes of a Circular Linked List | structure for a node ; Function to insert a node at the beginning of a Circular linked list ; If linked list is not null then set the next of last node ; For the first node ; Function to find sum of the given Circular linked list ; Initialize lists as empty ; Created linked list will be 11.2 .56 .12
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } function push ( head_ref , data ) { var ptr1 = new Node ( ) ; var temp = head_ref ; ptr1 . data = data ; ptr1 . next = head_ref ; if ( head_ref != null ) { while ( temp . next != head_ref ) temp = temp . next ; temp . next = ptr1 ; } else ptr1 . next = ptr1 ; head_ref = ptr1 ; return head_ref ; } function sumOfList ( head ) { var temp = head ; var sum = 0 ; if ( head != null ) { do { temp = temp . next ; sum += temp . data ; } while ( temp != head ) ; } return sum ; } var head = null ; head = push ( head , 12 ) ; head = push ( head , 56 ) ; head = push ( head , 2 ) ; head = push ( head , 11 ) ; document . write ( " " + " " + sumOfList ( head ) ) ;