题解 | #从单向链表中删除指定值的节点#

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

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

astr = list(map(int, input().split()))
n = astr[0]
head = astr[1]
data = astr[2:n*2]
del_node = astr[n*2:]
L = [head]

def insert_node(L, first_node, last_node):
    index = L.index(first_node)
    re = L[:index+1]
    re.append(last_node)
    re.extend(L[index+1:])
    return re

for i in range(n-1):
    first_node = data[2*i + 1]
    last_node = data[2*i]
    L = insert_node(L, first_node, last_node)

for d in del_node:
    if d in L:
        L.remove(d)

print(' '.join(map(str, L)))

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
牛客网
牛客企业服务