题解 | #输出单向链表中倒数第k个结点#

输出单向链表中倒数第k个结点

https://www.nowcoder.com/practice/54404a78aec1435a81150f15f899417d

class ListNode:
    def __init__(self, key=None) -> None:
        self.key = key
        self.next = None
    
    def push(self, val):
        if not self.key:
            self.key = val
        else:
            l = ListNode(val)
            cur = self
            while cur.next:
                cur = cur.next
            cur.next = l

while True:
    try:
        num = input()
        nodes = input().strip().split()
        tar = int(input().strip())

        l = ListNode()

        for i in nodes:
            l.push(i)

        res = []

        cur = l

        while cur:
            res.append(cur.key)
            cur = cur.next

        print(res[-tar])
    except:
        break

全部评论

相关推荐

__Offer__:认识的室友啥也不回细节,线下面联想大模型一次通关我给我干不回了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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