Rotate List
class Solution:
def rotateRight(self, head, k):
# ===================
# Check for edge cases: empty list or no rotation needed
# ===================
if not head or not head.next or k == 0:
return head
# ===================
# Find the length of the list
# ===================
N = 1
temp = head
while temp.next:
N += 1 # Increment length
temp = temp.next
# ===================
# Calculate the effective rotation offset
# ===================
k = k % N
if k == 0:
return head
else:
# make circular:
temp.next = head
# ===================
# Rotate the list
# ===================
for _ in range(N - k):
temp = temp.next
head = temp.next # new head
temp.next = None # new tail
return head