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.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
int countRotation( struct Node* head)
{
int count = 0;
int min = head->data;
while (head != NULL) {
if (min > head->data)
break ;
count++;
head = head->next;
}
return count;
}
void push( struct Node** head, int data)
{
struct Node* newNode = new Node;
newNode->data = data;
newNode->next = (*head);
(*head) = newNode;
}
void printList( struct Node* node)
{
while (node != NULL) {
printf ( "%d " , node->data);
node = node->next;
}
}
int main()
{
struct Node* head = NULL;
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: " ;
cout << countRotation(head) << endl;
return 0;
}
|
Java
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) + "\n" );
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def countRotation(head):
count = 0
min = head.data
while (head ! = None ):
if ( min > head.data):
break
count + = 1
head = head. next
return count
def push(head, data):
newNode = Node(data)
newNode.data = data
newNode. next = (head)
(head) = newNode
return head
def printList(node):
while (node ! = None ):
print (node.data, end = ' ' )
node = node. next
if __name__ = = '__main__' :
head = None
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);
print ()
print ( "Linked list rotated elements: " ,
end = '')
print (countRotation(head))
|
C#
using System;
class LinkedList{
Node head;
class Node
{
public int data;
public Node next;
public Node( int d)
{
data = d;
next = null ;
}
}
int countRotation()
{
int count = 0;
int min = head.data;
while (head != null ) {
if (min > head.data)
break ;
count++;
head = head.next;
}
return count;
}
public void push( int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
public void printList()
{
Node tnode = head;
while (tnode != null )
{
Console.Write(tnode.data + "->" );
tnode = tnode.next;
}
Console.WriteLine( "NULL" );
}
static public void Main()
{
LinkedList llist = new LinkedList();
llist.push(12);
llist.push(11);
llist.push(8);
llist.push(5);
llist.push(18);
llist.push(15);
llist.printList();
Console.Write( "Linked list rotated elements: " );
Console.WriteLine(llist.countRotation());
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .data=0;
this .next= null ;
}
}
function countRotation(head)
{
let count = 0;
let min = head.data;
while (head != null ) {
if (min > head.data)
break ;
count++;
head = head.next;
}
return count;
}
function push(head,data)
{
let newNode = new Node();
newNode.data = data;
newNode.next = (head);
(head) = newNode;
return head;
}
function printList(node)
{
while (node != null ) {
document.write(node.data+ " " );
node = node.next;
}
}
let 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);
document.write( "<br>" );
document.write( "Linked list rotated elements: " );
document.write(countRotation(head) + "<br>" );
</script>
|
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.
This article is contributed by Dharmendra Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.