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 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 C++ Program For Insertion Sort In A Singly Linked List We have discussed Insertion Sort for arrays. In this article we are going to discuss Insertion Sort for linked list. Below is a simple insertion sort algorithm for a linked list. 1) Create an empty sorted (or result) list. 2) Traverse the given list, do following for every node. ......a) Insert curr 5 min read C++ Program to Rotate Doubly linked list by N nodes Given a doubly linked list, rotate the linked list counter-clockwise by N nodes. Here N is a given positive integer and is smaller than the count of nodes in linked list. N = 2Rotated List: Examples: Input : a b c d e N = 2 Output : c d e a b Input : a b c d e f g h N = 4 Output : e f g h a b c d As 4 min read C++ Program to Count rotations divisible by 4 Given a large positive number as string, count all rotations of the given number which are divisible by 4. Examples: Input: 8 Output: 1 Input: 20 Output: 1 Rotation: 20 is divisible by 4 02 is not divisible by 4 Input : 13502 Output : 0 No rotation is divisible by 4 Input : 43292816 Output : 5 5 rot 3 min read C++ Program for Check if an array is sorted and rotated Given an array of N distinct integers. The task is to write a program to check if this array is sorted and rotated counter-clockwise. A sorted array is not considered as sorted and rotated, i.e., there should at least one rotation.Examples: Input : arr[] = { 3, 4, 5, 1, 2 } Output : YES The above ar 5 min read C++ Program to Count rotations divisible by 8 Given a large positive number as string, count all rotations of the given number which are divisible by 8. Examples: Input: 8 Output: 1 Input: 40 Output: 1 Rotation: 40 is divisible by 8 04 is not divisible by 8 Input : 13502 Output : 0 No rotation is divisible by 8 Input : 43262488612 Output : 4 Ap 3 min read Like