New update is available. Click here to update.
sidenav-btnClose
Topic list
Rotate Linked List
MEDIUM
25 mins
23 upvotes
Linked List
Topics (Covered in this problem)
Problem solved
Skill meter
Linked List
-
Other topics
Problem solved
Skill meter
Strings
-
Matrices (2D Arrays)
-
Sorting
-
Binary Search
-
Stacks & Queues
-
Trees
-
Graph
-
Dynamic Programming
-
Greedy
-
Tries
-
Arrays
-
Binary Search Trees
-
Heap
-
Bit Manipulation
-

Rotate Linked List

Contributed by
Ayush Thakur
Medium
Avg time to solve 25 mins
Success Rate 65 %
Share
23 upvotes

Problem Statement

You have been given a Linked List having ‘N’ nodes and an integer ‘K’. You have to rotate the Linked List by ‘K’ positions in a clockwise direction.

Example :

 Given Linked List : 1 2 3 4 -1 and K : 2
 Then the modified Linked List after K rotation : 3 4 1 2
Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1 :
2
1 2 3 4 5 6 -1
2
2 4 -1
3
Sample Output 1 :
5 6 1 2 3 4
4 2
Explanation For Sample Input 1 :
For the first test case, after 1st clockwise rotation the modified Linked List will be : 6 1 2 3 4 5
After, 2nd clockwise rotated the modified Linked List will be : 5 6 1 2 3 4

For the second test case, after 1st clockwise rotation the modified Linked List will be : 4 2
After, 2nd clockwise rotated the modified Linked List will be : 2 4
After, 3rd clockwise rotated the modified Linked List will be : 4 2
Sample Input 2 :
2
1 2 3 -1
2
3 6 9 -1
0
Sample Output 2:
2 3 1
3 6 9
Explanation For Sample Input 2 :
For the first test case, after 1st clockwise rotation the modified Linked List will be : 3 1 2
After, 2nd clockwise rotated the modified Linked List will be : 2 3 1

For the second test case, ‘K’ is 0 therefore there will be no rotation, so the Linked List remains unchanged.
Reset Code
Full screen
copy-code
Console