题解 | #【模板】链表#

【模板】链表

https://www.nowcoder.com/practice/97dc1ac2311046618fd19960041e3c6f

class node:
    def __init__(self, data=None, next=None):
        self.data = data
        self.next = next
class link:
    def __init__(self):
        self.head = node()
    def insert(self, x, y):
        a = self.head
        while True:
            b = a.next
            if b is None:
                a.next = node(y)
                return None
            elif b.data == x:
                c = node(y)
                a.next = c
                c.next = b
                return None
            a = b
    def delete(self, x):
        a = self.head
        while True:
            b = a.next
            if b is None:
                return None
            elif b.data == x:
                c = b.next
                a.next = c
                return None
            a = b
    def list(self):
        a = self.head
        b = []
        while True:
            a = a.next
            if a is None:
                return b
            b.append(a.data)

n = int(input())
a = link()
for i in range(n):
    s = input().split()
    if s[0] == 'insert':
        x, y = int(s[1]), int(s[2])
        #print(x,y)
        a.insert(x, y)
    else:
        x = int(s[1])
        a.delete(x)
res = a.list()
if len(res) == 0:
    print('NULL')
else:
    print(*res)

全部评论

相关推荐

11-22 16:49
已编辑
北京邮电大学 Java
美团 质效,测开 n*15.5
点赞 评论 收藏
分享
拒绝无效加班的小师弟很中意你:求职意向没有,年龄、课程冗余信息可以删掉,需要提升项目经历。排版需要修改。
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务