Ctrl K

Move the Tail to the Front

Medium
You are given the head of a singly linked list and an integer k. Do the following k times: detach the last node and put it at the front of the list. Return the head of the resulting list. k can be far larger than the list's length, so literally repeating the step k times will not do.
Example 1
head=list_node([10, 20, 30, 40]), k=1 → [40, 10, 20, 30]

The last node 40 moves to the front: [40, 10, 20, 30].

Example 2
head=list_node([5, 6]), k=3 → [6, 5]

Two steps bring the list back to where it was, so three steps are the same as one: [6, 5].

Example 3
head=list_node([]), k=7 → None

An empty list stays empty.

Constraints
  • 0 ≤ number of nodes ≤ 500
  • -100 ≤ Node.val ≤ 100
  • 0 ≤ k ≤ 2·10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsDrop the K-th Node From the Tail MediumSorted List, Unique Values Only MediumSplit a List Around a Pivot MediumList Folded From Both Ends MediumSorting a Linked List Medium
loading Python…
Input
head=list_node([10, 20, 30, 40]), k=1
Expected
[40, 10, 20, 30]

Run checks the first 3 tests in your browser. Submit checks all 102 tests on the server, hidden ones included.