Count rotations in sorted and rotated linked list | Program for count number of rotations in sorted 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 . ; Driver functions ; Create a node and initialize with null ; push ( ) insert node in linked list . 15.18 .5 . 8.11 . 12 ; Function call countRotation ( )
import java . util . * ; class GFG { static class Node { int data ; Node next ; } ; static int countRotation ( Node head ) { int count = 0 ; int min = head . data ; while ( head != null ) { if ( min > head . data ) break ; count ++ ; head = head . next ; } return count ; } static Node push ( Node head , int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . next = ( head ) ; ( head ) = newNode ; return head ; } static void printList ( Node node ) { while ( node != null ) { System . out . printf ( " % d ▁ " , node . data ) ; node = node . next ; } } public static void main ( String [ ] args ) { Node 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 ) ; System . out . println ( ) ; System . out . print ( " Linked ▁ list ▁ rotated ▁ elements : ▁ " ) ; System . out . print ( countRotation ( head ) + "NEW_LINE"); } }