题解 | #从单向链表中删除指定值的节点# 链式前向星做法

从单向链表中删除指定值的节点

http://www.nowcoder.com/practice/f96cd47e812842269058d483a11ced4f

allinput=list(map(int,input().strip().split()))
n=allinput[0]
head=allinput[1]
removenum=allinput[-1]
h=[-1 for _ in range(10010)]
def add(a,b):
    h[a]=b
for i in range(2,len(allinput)-2,2):
    tmpnext=h[allinput[i+1]]
    add(allinput[i+1],allinput[i])
    add(allinput[i],tmpnext)
res=[]
if head==removenum:
    head=h[head]
else:
    p=head
    while True:
        if h[p]==removenum:
            break
        p=h[p]
    h[p]=h[removenum]
p=head
while True:
    res.append(p)
    p=h[p]
    if p==-1:
        break
print(' '.join(list(map(str,res)))+' ')

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务