text: Reverse a doubly circular linked list | structure of a node of linked list ; function to create and return a new node ; Function to insert at the end ; If the list is empty , create a single node circular and doubly list ; Find last node / ; Start is going to be next of new_node ; Make new node previous of start ; Make last preivous of new node ; Make new node next of old last ; Uitlity function to revesre a doubly circular linked list ; Initialize a new head pointer ; get pointer to the the last node ; set ' curr ' to last node ; traverse list in backward direction ; insert ' curr ' at the end of the list starting with the ' new _ head ' pointer ; head pointer of the reversed list ; function to display a doubly circular list in forward and backward direction ; Driver code
code: class Node { constructor ( ) { this . data = 0 ; this . next = null ; this . prev = null ; } } function getNode ( data ) { var newNode = new Node ( ) ; newNode . data = data ; return newNode ; } function insertEnd ( head , new_node ) { if ( head == null ) { new_node . next = new_node . prev = new_node ; head = new_node ; return head ; } var last = head . prev ; new_node . next = head ; head . prev = new_node ; new_node . prev = last ; last . next = new_node ; return head ; } function reverse ( head ) { if ( head == null ) return null ; var new_head = null ; var last = head . prev ; var curr = last , prev ; while ( curr . prev != last ) { prev = curr . prev ; new_head = insertEnd ( new_head , curr ) ; curr = prev ; } new_head = insertEnd ( new_head , curr ) ; return new_head ; } function display ( head ) { if ( head == null ) return ; var temp = head ; document . write ( " " ) ; while ( temp . next != head ) { document . write ( temp . data + " " ) ; temp = temp . next ; } document . write ( temp . data + " " ) ; var last = head . prev ; temp = last ; document . write ( " " ) ; while ( temp . prev != last ) { document . write ( temp . data + " " ) ; temp = temp . prev ; } document . write ( temp . data + " " ) ; } var head = null ; head = insertEnd ( head , getNode ( 1 ) ) ; head = insertEnd ( head , getNode ( 2 ) ) ; head = insertEnd ( head , getNode ( 3 ) ) ; head = insertEnd ( head , getNode ( 4 ) ) ; head = insertEnd ( head , getNode ( 5 ) ) ; document . write ( " " ) ; display ( head ) ; head = reverse ( head ) ; document . write ( " " ) ; display ( head ) ;