Count rotations in sorted and rotated linked list | Linked list node ; Function that count number of rotation in singly linked list . ; declare count variable and assign it 1. ; declare a min variable and assign to data of head node . ; check that while head not equal to null . ; if min value is greater then head . data then it breaks the while loop and return the value of count . ; head assign the next value of head . ; Function to push element in linked list . ; Allocate dynamic memory for newNode . ; Assign the data into newNode . ; newNode . next assign the address of head node . ; newNode become the headNode . ; Display linked list . ; Create a node and initialize with null ; push ( ) insert node in linked list . 15.18 .5 . 8.11 . 12 ; Function call countRotation ( )
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } function countRotation ( head ) { let count = 0 ; let min = head . data ; while ( head != null ) { if ( min > head . data ) break ; count ++ ; head = head . next ; } return count ; } function push ( head , data ) { let newNode = new Node ( ) ; newNode . data = data ; newNode . next = ( head ) ; ( head ) = newNode ; return head ; } function printList ( node ) { while ( node != null ) { document . write ( node . data + " " ) ; node = node . next ; } } let head = null ; head = push ( head , 12 ) ; head = push ( head , 11 ) ; head = push ( head , 8 ) ; head = push ( head , 5 ) ; head = push ( head , 18 ) ; head = push ( head , 15 ) ; printList ( head ) ; document . write ( " " ) ; document . write ( " " ) ; document . write ( countRotation ( head ) + " " ) ;