在一行上:
先输入一个整数
代表链表中节点的总数;
随后输入一个整数
代表头节点的值;
随后输入
个二元组
;
最后输入一个整数
,代表需要删除的节点值。
除此之外,保证每一个
值在输入前已经存在于链表中;每一个
值在输入前均不存在于链表中。节点的值各不相同。
在一行上输出
个整数,代表删除指定元素后剩余的链表。
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数据范围:链表长度满足,节点中的值满足
测试用例保证输入合法
while True: try: ls = list(map(int,input().split())) # [5,2,3, 2, 4, 3, 5, 2, 1, 4,3] ls2 = ls[2:-1] # [3, 2, 4, 3, 5, 2, 1, 4] res = ls[1:2] # [2] for i in range(1,len(ls2),2): if ls2[i] in res and ls2[i-1] not in res: res.insert(res.index(ls2[i])+1,ls2[i-1]) # print(res) res.remove(ls[-1]) for i in res: print(i,end=' ') except: break
l = list(map(int, input().split()))
n = l[0]
listhead = l[1]
lianbiao = [listhead]
for i in range(0, 2*(n-1), 2):
tmpl = lianbiao[:]
for j in tmpl:
if l[2 + i] == j:
lianbiao.insert(lianbiao.index(j), l[3 + i])
elif l[3 + i] == j:
lianbiao.insert(lianbiao.index(j)+1, l[2 + i])
if len(l) == 2*n+1:
s = l[-1]
lianbiao.remove(s)
print(' '.join(map(str, lianbiao)))
else:
print(' '.join(map(str, lianbiao))) ss=input().strip().split()
zongjiedian=int(ss[0])
toujiedian=int(ss[1])
jiedian=list(map(int,ss[2:len(ss)-1]))
shanchu=int(ss[len(ss)-1])
youtou=[]
wutou=[]
for i in range(len(jiedian)//2):
zanshi=[jiedian[i*2],jiedian[i*2+1]]
if toujiedian in zanshi:
zanshi.pop(zanshi.index(toujiedian))
youtou.append([zanshi[0],toujiedian])
else:
wutou.append([jiedian[i*2],jiedian[i*2+1]])
lianbiao=[toujiedian]
for ele in youtou:
lianbiao.insert(1,ele[0])
while True:
if len(wutou)==0:
break
for ele in wutou:
wei=ele[0]
tou=ele[1]
if wei in lianbiao and tou in lianbiao:
wutou.remove(ele)
break
elif wei in lianbiao and tou not in lianbiao:
lianbiao.insert(lianbiao.index(wei),tou)
wutou.remove(ele)
break
elif tou in lianbiao and wei not in lianbiao:
lianbiao.insert(lianbiao.index(tou)+1,wei)
wutou.remove(ele)
break
lianbiao.remove(shanchu)
print(' '.join(map(str,lianbiao))) while True:
try:
s = input().split(' ')
n, link, d = s[0], [s[1]], s[-1]
groups = s[2:-1]
for i in range(0, len(groups), 2):
r, l = groups[i], groups[i+1]
inx = link.index(l) + 1
link = link[:inx] + [r] + link[inx:]
dinx = link.index(d)
link = link[:dinx]+link[dinx+1:]
print(' '.join(link))
except:
break raw = list(map(int, input().split())) l = raw[0] lis = [raw[1]] for i in range(l-1): tmp = raw[i*2+2:i*2+4] for j in range(len(lis)): if lis[j] == tmp[1]: lis.insert(j+1, tmp[0]) break for i in lis: if i != raw[-1]: print(i, end=' ')
l = list(map(int, input().split())) n = l[0] head = l[1] dele = l[-1] li = l[2:-1] lis = [] num = [0 for i in range(n)] num[0] = head for i in range(0, len(li), 2): lis.append([li[i], li[i+1]]) for i in lis: idx = num.index(i[1]) num.insert(idx+1, i[0]) num.remove(dele) for i in num: if i != 0: print(i, end=' ')
import sys
class L:
def __init__(self,val=0) -> None:
self.val = val
self.next = None
l = input().split()
L1 = [l[1]]
rm = l[-1]
l = l[2:-1]
for i in range(1,len(l),2):
L1.insert(L1.index(l[i]),l[i-1])
L1 = L1[::-1]
for i in range(len(L1)):
if L1[i] == rm:
L1.pop(i)
break
print(' '.join(L1)) class list_node:
def __init__(self,value,next=None) -> None:
self.value=value
self.next=next
inputs=input().strip().split(' ')
n=inputs[0]
move=inputs[2:-1]
del_node=inputs[-1]
head=list_node(inputs[1])
for i in range(0,len(move),2):
p=head
new=list_node(move[i])
while p is not None:
if p.value==move[i+1]:
new.next=p.next
p.next=new
break
p=p.next
if head.value==del_node:
head=head.next
else:
p=head
while p is not None:
if p.next.value==del_node:
temp=p.next
p.next=temp.next
temp.next=None
break
p=p.next
p=head
while p is not None:
print(p.value,end=' ')
p=p.next
my_list = list(map(int, input().split())) head_list = [my_list[1]] mid_list = my_list[2:-1] for i in range(len(mid_list) // 2): head_list.insert(head_list.index(mid_list[2 * i + 1]), mid_list[2 * i]) head_list.remove(my_list[-1]) for i in head_list[::-1]: print(i,end=' ')