2 条题解

  • 2
    @ 2024-9-17 20:32:02
    class Node:
        def __init__(self,val:int,nxt:int):
            self.val=val
            self.nxt=nxt
    def add (it,val):
        a.append(Node(val,a[it].nxt))
        a[it].nxt=len(a)-1
    def delete (it):
        a[it].nxt=a[a[it].nxt].nxt
    a=[Node(0,0) for i in range(3000)]
    n,h,key=map(int,input().split())
    for i in range(n):
        a[i].val,a[i].nxt=map(int,input().split())
    it=h
    if a[h].val==key:
        h=a[h].nxt#特判
    while it!=-1:
        if a[a[it].nxt].val==key:
            delete(it)
        it=a[it].nxt
    it=h
    while it!=-1:
        print(a[it].val,end=" ")
        it=a[it].nxt
    

    题解qp 代码仅供参考,课上讲的实现方式还是要会的,class写法较好写好理解

    • 1
      @ 2024-9-20 14:38:11
      n,head,key=map(int,input().split())
      a=[]
      for i in range(n):
          x,y=map(int,input().split())
          a.append([x,y])
      a.append([key,-1])
      p=head;pre=-1
      while p!=-1 and key!=a[p][0]:
          pre=p
          p=a[p][1]
      if pre==-1:
          head=a[head][1]
      else:
          a[pre][1]=a[p][1]
      p=head
      while p!=-1:
          print(a[p][0],end=" ")
          p=a[p][1]
      
      • 1

      信息

      ID
      942
      时间
      1000ms
      内存
      256MiB
      难度
      6
      标签
      递交数
      158
      已通过
      44
      上传者