题解 | #输出单向链表中倒数第k个结点#
输出单向链表中倒数第k个结点
https://www.nowcoder.com/practice/54404a78aec1435a81150f15f899417d
class Node: # 节点 def __init__(self, v): self.v = v self.next = None class SingleLinkList: # 链表 def __init__(self): # 初始化空 self.head = None def append(self, i): n = Node(i) # 空 if not self.head: self.head = n return cur = self.head while cur.next: cur = cur.next cur.next = n n.next = None def pop(self): # 1. 只有1个节点 cur = self.head if cur.next == None: r= self.head.v self.head = self.head.next return r # 2. 非头部 while 1: cur_next = cur.next if cur_next.next: cur = cur.next else: r = cur.next.v cur.next = None break return r if __name__ == '__main__': while True: try: # input n = int(input()) lst = list(map(int, input().split())) k = int(input()) # in SingleLinkList L = SingleLinkList() # get answer for i in lst: L.append(i) lst01 = [] for j in range(k): lst01.append(L.pop()) print(lst01[-1]) except: break