C++ Program to Count rotations in sorted and rotated linked list Last Updated : 27 May, 2022 Comments Improve Suggest changes Like Article Like Report Given a linked list of n nodes which is first sorted, then rotated by k elements. Find the value of k. The idea is to traverse singly linked list to check condition whether current node value is greater than value of next node. If the given condition is true, then break the loop. Otherwise increase the counter variable and increase the node by node->next. Below is the implementation of this approach. C++ // Program for count number of rotations in // sorted linked list. #include <bits/stdc++.h> using namespace std; /* Linked list node */ struct Node { int data; struct Node* next; }; // Function that count number of // rotation in singly linked list. int countRotation(struct Node* head) { // declare count variable and assign it 1. int count = 0; // declare a min variable and assign to // data of head node. int min = head->data; // check that while head not equal to NULL. while (head != NULL) { // if min value is greater then head->data // then it breaks the while loop and // return the value of count. if (min > head->data) break; count++; // head assign the next value of head. head = head->next; } return count; } // Function to push element in linked list. void push(struct Node** head, int data) { // Allocate dynamic memory for newNode. struct Node* newNode = new Node; // Assign the data into newNode. newNode->data = data; // newNode->next assign the address of // head node. newNode->next = (*head); // newNode become the headNode. (*head) = newNode; } // Display linked list. void printList(struct Node* node) { while (node != NULL) { printf("%d ", node->data); node = node->next; } } // Driver functions int main() { // Create a node and initialize with NULL struct Node* head = NULL; // push() insert node in linked list. // 15 -> 18 -> 5 -> 8 -> 11 -> 12 push(&head, 12); push(&head, 11); push(&head, 8); push(&head, 5); push(&head, 18); push(&head, 15); printList(head); cout << endl; cout << "Linked list rotated elements: "; // Function call countRotation() cout << countRotation(head) << endl; return 0; } Output15 18 5 8 11 12 Linked list rotated elements: 2 Time Complexity: O(N), where N represents the length of the linked list.Auxiliary Space: O(1), no extra space is required, so it is a constant. Please refer complete article on Count rotations in sorted and rotated linked list for more details! Comment More infoAdvertise with us Next Article C++ Program to Count rotations in sorted and rotated linked list K kartik Follow Improve Article Tags : Linked List C++ Programs C++ DSA rotation +1 More Practice Tags : CPPLinked List Similar Reads C++ Program to Count of rotations required to generate a sorted array Given an array arr[], the task is to find the number of rotations required to convert the given array to sorted form.Examples: Input: arr[] = {4, 5, 1, 2, 3}Â Output: 2Â Explanation:Â Sorted array {1, 2, 3, 4, 5} after 2 anti-clockwise rotations. Input: arr[] = {2, 1, 2, 2, 2}Â Output: 1Â Explanation:Â So 4 min read C Program to Rotate Linked List block wise Given a Linked List of length n and block length k rotate in a circular manner towards right/left each block by a number d. If d is positive rotate towards right else rotate towards left. Examples:Â Input: 1->2->3->4->5->6->7->8->9->NULL, k = 3 d = 1 Output: 3->1->2->6->4->5->9->7->8->NULL Explanati 4 min read C++ Program to Rotate Linked List block wise Given a Linked List of length n and block length k rotate in a circular manner towards right/left each block by a number d. If d is positive rotate towards right else rotate towards left. Examples:Â Input: 1->2->3->4->5->6->7->8->9->NULL, k = 3 d = 1 Output: 3->1->2->6->4->5->9->7->8->NULL Explanati 4 min read C++ Program For Rotating A Linked List Given a singly linked list, rotate the linked list counter-clockwise by k nodes. Where k is a given positive integer. For example, if the given linked list is 10->20->30->40->50->60 and k is 4, the list should be modified to 50->60->10->20->30->40. Assume that k is smal 6 min read C++ Program to Count rotations required to sort given array in non-increasing order Given an array arr[] consisting of N integers, the task is to sort the array in non-increasing order by minimum number of anti-clockwise rotations. If it is not possible to sort the array, then print "-1". Otherwise, print the count of rotations. Examples: Input: arr[] = {2, 1, 5, 4, 3}Output: 2Expl 3 min read Like