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

while True:
    try:
        s = list(map(int, input().split()))
        gs = s[0]
        sc = s[-1]
        kt = s[1]
        s = s[2:-1]
        lb = [kt]  # 结果初始值为第一个
        for i in range(0, len(s) - 1, 2):
            x = s[i]
            y = s[i + 1]
            # 判断是不是末尾,末尾直接加
            if lb.index(y) == len(lb) - 1:
                lb.append(x)
                # print('mowei = ' + str(lb))
            elif lb.index(y) == 0: # 判断在首位
                lq = [lb[0]]
                lh = lb[1:]
                lb = lq + [x] + lh
                # print('shouwei = ' + str(lb))
            else:
                y_index = lb.index(y)
                lq = lb[:y_index + 1]
                lh = lb[y_index + 1:]
                lb = lq + [x] + lh
                # print('qita = ' + str(lb))
            # print('zuizhong = ' + str(lb))

        # 得到链表,开始删除
        lb.remove(sc)
        print(' '.join(map(str, lb)))
    except:
        break

全部评论

相关推荐

尊尼获获:闺蜜在哪?
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务