题解 | 链表中倒数最后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

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务