Login light

快慢指针

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def kthToLast(self, head: ListNode, k: int) -> int:
        fast = head
        slow = head
        while k - 1 > 0:
            fast = fast.next
            k-=1
        while fast.next != None:
            fast = fast.next
            slow = slow.next
        return slow.val