Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Rotate List

Medium
linked-listtwo-pointers

Rotate the list to the right by k places: the last k nodes move to the front (in order).

Input

  • Line 1: space-separated integers — the list values (may be empty)
  • Line 2: the integer k

Output: the rotated list's values, space-separated.

Example 1
Input:
1 2 3 4 5
2
Output:
4 5 1 2 3
Example 2
Input:
0 1 2
4
Output:
2 0 1

k=4 on length 3 is k=1.

Constraints

  • 0 ≤ list length ≤ 500
  • 0 ≤ k ≤ 2·10^9
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.