题解 | #输出单向链表中倒数第k个结点#
输出单向链表中倒数第k个结点
http://www.nowcoder.com/practice/54404a78aec1435a81150f15f899417d
class Node:
def __init__(self, val, Next=None):
self.val = val
self.next = Next
class LNode:
def __init__(self, head):
self.head = Node(head)
def link(self, vl):
cur = self.head
while cur.next:
cur = cur.next
for v in vl:
cur.next = Node(v)
cur = cur.next
def findk(self, k):
cur = self.head
pre = self.head
k1 = 0
k2 = 0
while cur:
cur = cur.next
k1+=1
if k1 - k2 == k+1:
k2+=1
pre = pre.next
return pre.val
while True:
try:
n= int(input())
a = list(map(int, input().split()))
k = int(input())
ln = LNode(a[0])
ln.link(a[1:n])
v = ln.findk(k)
print(str(v))
except:
break