# Count rotations in sorted and rotated linked list

Given a linked list of n nodes which is first sorted, then rotated by k elements. Find the value of k.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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.

```
// Program for count number of rotations in
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};

// Function that count number of
// rotation in singly linked list.
{
// declare count variable and assign it 1.
int count = 0;

// declare a min variable and assign to

// 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.
break;

count++;

}
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

}

void printList(struct Node* node)
{
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
}

// Drier functions
int main()
{
// Create a node and initialize with NULL

// push() insert node in linked list.
// 15 -> 18 -> 5 -> 8 -> 11 -> 12

cout << endl;
cout << "Linked list rotated elements: ";

// Function call countRotation()

return 0;
}
```

Output:

```Linked List:
15 18 5 8 11 12
```

This article is contributed by Dharmendra Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
1.5 Average Difficulty : 1.5/5.0
Based on 8 vote(s)