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 None . ; 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 . ; Driver code ; Create a node and initialize with None ; push ( ) insert node in linked list . 15 -> 18 -> 5 -> 8 -> 11 -> 12 ; Function call countRotation ( )
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def countRotation ( head ) : NEW_LINE INDENT count = 0 NEW_LINE min = head . data NEW_LINE while ( head != None ) : NEW_LINE INDENT if ( min > head . data ) : NEW_LINE INDENT break NEW_LINE DEDENT count += 1 NEW_LINE head = head . next NEW_LINE DEDENT return count NEW_LINE DEDENT def push ( head , data ) : NEW_LINE INDENT newNode = Node ( data ) NEW_LINE newNode . data = data NEW_LINE newNode . next = ( head ) NEW_LINE ( head ) = newNode NEW_LINE return head NEW_LINE DEDENT def printList ( node ) : NEW_LINE INDENT while ( node != None ) : NEW_LINE INDENT print ( node . data , end = ' ▁ ' ) NEW_LINE node = node . next NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None NEW_LINE head = push ( head , 12 ) NEW_LINE head = push ( head , 11 ) NEW_LINE head = push ( head , 8 ) NEW_LINE head = push ( head , 5 ) NEW_LINE head = push ( head , 18 ) NEW_LINE head = push ( head , 15 ) NEW_LINE printList ( head ) ; NEW_LINE print ( ) NEW_LINE print ( " Linked ▁ list ▁ rotated ▁ elements : ▁ " , end = ' ' ) NEW_LINE print ( countRotation ( head ) ) NEW_LINE DEDENT