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

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

全部评论

相关推荐

点赞 评论 收藏
分享
每晚夜里独自颤抖:你cet6就cet6,cet4就cet4,你写个cet证书等是什么意思。专业技能快赶上项目行数,你做的这2个项目哪里能提现你有这么多技能呢
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务