from utils.list_utils import *

class Solution:
    def rotateRight(self, head: ListNode, k: int) -> ListNode:
        if k == 0 or head is None:
            return head

        # 让快指针先跑起来，也找k是否需要对长度取余
        ptr_q = head  # 快慢指针
        k_bt_llen = False
        list_len = 0
        for i in range(k):
            list_len += 1
            ptr_q = ptr_q.next
            if ptr_q is None:
                k_bt_llen = True
                k = k % list_len
                break

        # 指针跑起来
        ptr_s = head
        if k_bt_llen:
            if k == 0:
                return head
            ptr_q = head
            i = 0
        else:
            i = k
        while True:
            if ptr_q.next is None:
                break
            ptr_q = ptr_q.next
            if i >= k:
                ptr_s = ptr_s.next
            i+=1
        ptr_q.next = head
        new_head = ptr_s.next
        ptr_s.next = None
        return new_head

sol = Solution()
# rlt=sol.rotateRight(create_link_list([1,2,3,4,5]), 2)
# print_link_list(rlt)
# rlt=sol.rotateRight(create_link_list([0,1,2]), 4)
# print_link_list(rlt)
rlt=sol.rotateRight(create_link_list([1,2]), 1)
print_link_list(rlt)