题解 | 输出单向链表中倒数第k个结点
输出单向链表中倒数第k个结点
https://www.nowcoder.com/practice/54404a78aec1435a81150f15f899417d
class Node:
def __init__(self,val):
self.val=val
self.next=None
def setNext(self,newnext):
self.next=newnext
def getNext(self):
return self.next
class Unorderedlist:
def __init__(self):
self.head=None
def isEmpty(self):
return self.head == None
def add(self,item):
temp = Node(item)
temp.setNext(self.head)
self.head=temp
def pos_search(self,pos):
count=0
item=self.head
while item!=None:
count+=1
if count!=N-pos+1:
item=item.next
else:
return item.val
while True:
try:
N,lst_1,k=int(input()),input().split()[::-1],int(input())
un_lst=Unorderedlist()
for i in range(N):
n=lst_1[i]
un_lst.add(n)
a=un_lst.pos_search(k)
print(a)
except:
break