classSolution: defrotateRight(self, head: ListNode, k: int) -> ListNode: ifnot head: # 判空 return head cnt, l = 0, 0 p = head # 计算链表长度 while p: p = p.next l += 1 p = head left = p # 链表右移的部分 k = k % l if k % l else l # 取模 while p: cnt += 1 if cnt == l - k: break p = p.next ifnot p: # 移到距离刚好等于链表长度的话相当于没动 return head right = p.next# 链表左移的部分 p.next = None p = right while p.next: p = p.next p.next = left return right