Rotate a Linked List
Submissions: 29448   Accuracy:

33.33%

  Difficulty: Easy   Marks: 2
     

Given a singly linked list of size N. The task is to rotate the linked list counter-clockwise by k nodes, where k is a given positive integer smaller than or equal to length of the linked list.

Input:
First line of input contains number of testcases T. For each testcase, first line of input contains length of linked list and next line contains linked list elements and last line contains k, by which linked list is to be rotated.

Output:
For each testcase, print the rotated linked list.

User Task:
The task is to complete the function rotate() which takes head reference as the first argument and k as the second argument.

Constratints:
1 <= T <= 100
1 <= N <= 103
1 <= k <= 103

Example:
Input:
1
8
1 2 3 4 5 6 7 8
4

Output:
5 6 7 8 1 2 3 4

Explanation:
Testcase 1:
After rotating the linked list by 4 elements (anti-clockwise), we reached to node 5, which is (k+1)th node from beginning, now becomes head and tail of the linked list is joined to the previous head.

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Harshit Sidhwa,Saksham Raj Seth
Author: Karan Grover


Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.