题解 | 链表中倒数最后k个结点
链表中倒数最后k个结点
https://www.nowcoder.com/practice/886370fe658f41b498d40fb34ae76ff9
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def FindKthToTail(self , pHead: ListNode, k: int) -> ListNode:
first = pHead
second = pHead
n = 0
while n < k and first:
first = first.next
n += 1
if first is None and n < k:
return None
while first:
second = second.next
first = first.next
return second
