


Input: linked list = [1 2 3 4] , k = 2
Output: 3 4 1 2
Explanation:
We have to rotate the given linked list to the right 2 times. After rotating it to the right once it becomes 4->1->2->3. After rotating it to the right again, it becomes 3->4->1->2.
The first line of the input contains a single integer 'n', denoting the number of nodes in the linked list.
The next line contains 'n' space-separated integers, denoting the elements of the linked list.
The next line contains an integer ‘k’, representing the number of positions up to the given linked list that has to rotate.
Return the head of the linked list after rotating it.
You do not need to print anything, it has already been taken care of. Just implement the given function. Elements of your returned linked list will be printed in a single line.
Find the length of the Linked List to check whether the ‘K’ is greater than the Length of the Linked List or not. Take a modulo of ‘K’ with its length if it is greater than the length. Reach the (‘K’+1)th node from last and change the pointers of nodes to get a rotated Linked List.
Here is the algorithm:
The basic idea of this approach is to first convert a linked list to circular linked list by making the last pointer point to head. Change the pointers of (K+1)th node to get a rotated linked list.
The steps are as follows :