单向链表倒数第 k 个结点(Python)
输出单向链表中倒数第k个结点
http://www.nowcoder.com/questionTerminal/54404a78aec1435a81150f15f899417d
解法一:
装作没看到,当数组解 :)
while True: try: l, s, k = int(input()), input().split(), int(input()) print(s[l - k] if k else 0) except: break
解法二:
自己定义节点再连成链表咯 :)
class Node(object): def __init__(self, val=0): self.val = val self.next = None while True: try: l, s, k, head = int(input()), list(map(int, input().split())), int(input()), Node() while k: head.next = Node(s.pop()) head = head.next k -= 1 print(head.val) except: break
提交结果