题解 | #输出单向链表中倒数第k个结点#
输出单向链表中倒数第k个结点
https://www.nowcoder.com/practice/54404a78aec1435a81150f15f899417d
import sys #for line in sys.stdin: # a = line.split() # print(int(a[0]) + int(a[1])) #定义类节点的函数 class ListNode(object): def __init__(self,x) : self.val= x self.next=None #定义打印倒数第K个节点的函数 #循环捕获n,s2,k的值,并进行打印第K个节点的输出 def printK(): while True: try: n = int(input()) s2 = input().split() k= int(input()) #print(s,s2,k) #用列表的切片取倒数k个的值 s2=s2[n-k:] res=ListNode(s2[0]) cur=res for x in s2[1:]: #先在尾部添加节点 cur.next=ListNode(x) #然后将当前节点移动到新添加的节点的位置 cur=cur.next print(res.val) except: break printK() #printK()