Rotate Linked List
Mediumjavapythonccppjavascript
Given a linked list and an integer K, move the last K nodes to the beginning of the list.
Input Format
The first line contains two integers N and K. The second line contains N space-separated integers.
Output Format
Print the rotated list from head to tail, separated by single spaces.
Example 1
Input
5 2 1 2 3 4 5
Output
4 5 1 2 3
Explanation: The last two nodes move to the front.
- 1 <= N <= 100000
- 0 <= K <= 1000000000
- -1000000000 <= value <= 1000000000
Submit an attempt to unlock the solution. (Sign in first.)
Related Problems
- Detect Cycle in Linked ListMedium · java, python, c, cpp, javascriptSolve Problem
- Add Two Numbers Using Linked ListsMedium · java, python, c, cpp, javascriptSolve Problem
- Intersection Point of Two Linked ListsMedium · java, python, c, cpp, javascriptSolve Problem