题解 | #【模板】链表#

【模板】链表

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)

全部评论

相关推荐

10-31 21:01
武汉大学 Java
lulululula...:仅仅按我个人的经历来看,大厂其实很少特别关注微服务,一般对微服务架构,限流熔断降级的概念了解就行,简历不写也不容易被问到。现在这个势头不如站点agent应用,比如做做mcp,rag,r对话agent,记忆管理之类的,说不定可以蹭上一波热度,进公司虽然可能还是干agent的杂活,但是可以学一学组内的业务和技术了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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