[
linked list
]
Leetcode 0061. Rotate List
https://leetcode.com/problems/rotate-list
What we need to do in this problem is to find k
-th element from the end, cut list in two parts and put second part after the first one. Let us do in it several steps:
- Find
n
, length of our list, just traverse it until we reached end. - If
k % n == 0
, we do not need to rotate list, so return it as it is. - Find element where we need to cut our list: it has number
n - k%n
, but we need to cut previous connection, so we stop one element earlier. - Finally, put new head as
middle.next
, make connection between head and tail of original ist and remove connection betweenmiddle
and its next, return new head.
Complexity is O(n)
for time: we traverse our list twice at most; space complexity is O(1)
, we did not use any additional space.
class Solution:
def rotateRight(self, head, k):
if not head or not head.next: return head
last, n = head, 1
while last.next:
last = last.next
n += 1
if k % n == 0: return head
middle = head
for i in range(n - k%n-1):
middle = middle.next
new_head = middle.next
last.next = head
middle.next = None
return new_head
If you like the solution, you can upvote it on leetcode discussion section: Problem 0061