题解 | #输出单向链表中倒数第k个结点#
输出单向链表中倒数第k个结点
https://www.nowcoder.com/practice/54404a78aec1435a81150f15f899417d
class Node(object): def __init__(self, val) -> None: self.val = val self.next = None while True: try: n = input() array = list(map(int, input().split(" "))) k = int(input()) # 正序构建单向链表 head = Node(0) p = head for num in array: p.next = Node(num) p = p.next # 使用快慢指针的方式寻找倒数k的元素 fast, slow = head, head for i in range(k): fast = fast.next while fast != None: slow = slow.next fast = fast.next print(slow.val) except: break