在一行上:
先输入一个整数
代表链表中节点的总数;
随后输入一个整数
代表头节点的值;
随后输入
个二元组
;
最后输入一个整数
,代表需要删除的节点值。
除此之外,保证每一个
值在输入前已经存在于链表中;每一个
值在输入前均不存在于链表中。节点的值各不相同。
在一行上输出
个整数,代表删除指定元素后剩余的链表。
5 2 3 2 4 3 5 2 1 4 3
2 5 4 1
在这个样例中,链表的构造过程如下:
头节点为
,得到链表
;
在
后插入
,得到链表
;
在
后插入
,得到链表
;
在
后插入
,得到链表
;
在
后插入
,得到链表
;
随后,删除值为
的节点,得到链表
。
6 2 1 2 3 2 5 1 4 5 7 2 2
7 3 1 5 4
在这个样例中,链表的构造过程如下:
头节点为
,得到链表
;
在
后插入
,得到链表
;
在
后插入
,得到链表
;
在
后插入
,得到链表
;
在
后插入
,得到链表
;
在
后插入
,得到链表
;
随后,删除值为
的节点,得到链表
。
本题由牛客重构过题面,您可能想要阅读原始题面,我们一并附于此处。
【以下为原始题面】
输入一个单向链表和一个节点的值,从单向链表中删除等于该值的节点,删除后如果链表中无节点则返回空指针。
链表的值不能重复。
构造过程,例如输入一行数据为:6 2 1 2 3 2 5 1 4 5 7 2 2则第一个参数6表示输入总共6个节点,第二个参数2表示头节点值为2,剩下的2个一组表示第2个节点值后面插入第1个节点值,为以下表示:1 2 表示为2->1链表为2->13 2表示为2->3链表为2->3->15 1表示为1->5链表为2->3->1->54 5表示为5->4链表为2->3->1->5->47 2表示为2->7链表为2->7->3->1->5->4最后的链表的顺序为 2 7 3 1 5 4最后一个参数为2,表示要删掉节点为2的值删除 结点 2
则结果为 7 3 1 5 4数据范围:链表长度满足,节点中的值满足
测试用例保证输入合法
def createlink(s:list,head,delete_n): linknode = [head] for v in s : for l in linknode: if v[1] == l: index_l = linknode.index(l) linknode.insert(index_l+1,v[0]) linknode.remove(delete_n) for v in linknode: print(v,end = ' ') l = list(map(int,input().split())) num = l[0] head = l[1] delete_n = l[-1] s = [] #存储二元组 for i in range(2,len(l)-1,2): s.append((l[i],l[i+1])) createlink(s,head,delete_n)
class ListNode: def __init__(self, val, next = None): self.val = val self.next = next class Solution: def insert(self, head, val_pre, val): cur = head while cur.val != val_pre: cur = cur.next next_node = cur.next node = ListNode(val) cur.next = node node.next = next_node def remove(self, head, val): cur, pre = head, head while cur.val != val: pre = cur cur = cur.next pre.next = cur.next input = list(map(int, input().split())) head = ListNode(input[1]) solution = Solution() for i in range(2, len(input) - 1, 2): solution.insert(head, input[i+1], input[i]) solution.remove(head, input[-1]) while head: print(head.val, end = ' ') head = head.next
求大佬帮忙看看,为啥超时了
import re import sys input_list = input().split() n = int(input_list[0]) # 链表结点个数 first_value = input_list[1] # 头结点的值 values = input_list[2:(2*n)] # 节点规则list x = input_list[-1] # 要删除的结点的值 LIST = [first_value] # 链表list while len(LIST)<n: # 当链表节点个数达到时,跳出循环 tmp = values.copy() # 复制一个节点规则list,防止后面循环里删除时产生矛盾 length = len(tmp) last_value = LIST[-1] # 从未完成的链表的最后一个节点开始,找其后面的值 # 需要倒序看节点规则list,比如头节点的值在节点规则里面出现三次,即头节点后面依次加入了3个节点,并且是倒叙加入 # 节点规则list里,偶数i对应i-1,i是定位需要添加后一个值的节点,i-1是添加的节点值 for i in range(length-1,-1,-2): if tmp[i] == last_value: LIST.append(tmp[i-1]) del values[i] # 为了减少遍历的长度,已经添加过的节点,在节点规则list里删除 del values[i-1] # 为了减少遍历的长度,已经添加过的节点值,在节点规则list里删除 result = '' for v in LIST: if v != x: result += str(v) result += ' ' print(result)
# 定义链表结构 class ListNode: def __init__(self,val,next=None): self.val = val self.next = next def addNode(old,new): newNode = ListNode(new) newNode.next = old.next old.next = newNode # 构建初始链表 inList = [int(i) for i in input().split()] n = inList[0] k = inList[-1] dummy =ListNode(None) root = ListNode(inList[1]) dummy.next = root #构建完整链表 for i in range(2,len(inList)-2,2): old = inList[i+1] new = inList[i] while root.val != old: root = root.next addNode(root,new) root = dummy.next #删除指定结点 if root.val == k: root = root.next else: for _ in range(n): if root.next.val == k: root.next = root.next.next break root = root.next #打印删除后链表 out = [] for _ in range(n-1): out.append(dummy.next.val) dummy = dummy.next for i in range(len(out)): print(out[i], end=' ')
class ListNode: def __init__(self, val): self.val = val self.next = None # 根据题意在链表中插入节点 def create_list(head_node, val1, val2): cur=head_node while cur: if cur.val==val2: insert_node=ListNode(val1) insert_node.next=cur.next cur.next=insert_node break else: cur=cur.next S=[int(x) for x in input().split()] head_value=S[1] #头结点值 N=S[2:-1] #链表的操作 value=S[-1] #要删除的节点的值 head=ListNode(head_value) # 按照题目格式插入各个节点 for i in range(0,len(N),2): create_list(head,N[i],N[i+1]) # 删除指定值的结点 cur=head while cur: if cur.next.val==value: cur.next=cur.next.next break else: cur=cur.next # 打印删除后的列表 cur=head while cur: print(cur.val,end=' ') cur=cur.next # 若删除后列表无结点 返回None if S[0]==1 and S[-1]==S[1]: print(None)
class ListNode: def __init__(self, val): self.val = val self.next = None while True: try: numbers = list(map(int, input().split())) count, start_value, del_value = numbers[0], numbers[1], numbers[-1] table = numbers[2:-1] length_table = len(table) start_node = ListNode(start_value) for i in range(0,length_table,2): next_value = table[i] next_node = ListNode(next_value) cur_value = table[i+1] head = start_node while head.val != cur_value: head = head.next if head.next: next_node.next = head.next head.next = next_node else: head.next = next_node head = start_node output = [] while head: if head.val != del_value: output.append(str(head.val)) head = head.next output = " ".join(output) print(output) except: break
class node: def __init__(self, data): self.data = data self.nextNode = None class lineTest: def __init__(self, fNode): self.fNode = fNode def insertNode(self, data1, data2): xNode = self.findNode(self.fNode, data2) newNode = node(data1) newNode.nextNode = xNode.nextNode xNode.nextNode = newNode def findNode(self, sNode, data): if sNode.data == data: return sNode else: return self.findNode(sNode.nextNode, data) def printLine(self, sNode, listC, rData): if sNode is None: return else: if sNode.data == rData: pass else: listC.append(sNode.data) self.printLine(sNode.nextNode, listC, rData) if __name__ == "__main__": a = input() listA = a.strip().split(" ") listB = [] for i in listA: listB.append(int(i)) nodeList = listB[2:len(listB) - 1] # 创建初始节点,并且创建链表类 nNode = node(listB[1]) lineClass = lineTest(nNode) # 链表类加入节点 i = 0 while i < len(nodeList): lineClass.insertNode(nodeList[i], nodeList[i + 1]) i += 2 listC = [] lineClass.printLine(nNode, listC, listB[-1]) resStr = "" for i in listC: resStr = resStr + " " + str(i) print(resStr.strip())
while True: try: s = input().split() s_num, s_top, ss, s_d = s[0], s[1], s[2:-1], s[-1] print( s_num, s_top, ss, s_d ) ssr = [s_top] for x in range(0, len(ss), 2): x1, x2 = ss[x], ss[x+1] ssr.insert(ssr.index(x2) + 1, x1) ssr.remove(s_d) print(' '.join( ssr )) except: break