AcWing 1587. 链表重复数据删除
原题链接
中等
作者:
aac
,
2024-11-25 17:57:06
,
所有人可见
,
阅读 1
N = int(1e5 + 10)
e = [0] * N;ne = [0] * N
h,n = map(int,input().split())
for i in range(n):
addr,data,next = map(int,input().split())
e[addr] = data
ne[addr] = next
list1 = []
list2 = []
se = set()
i = h
while i != -1:
val = e[i]
if abs(val) in se: # 如果这个数的绝对值在之前已经出现过了,则该节点要被删掉
list2.append([i,val])
else:
list1.append([i,val])
se.add(abs(val)) # 将该数绝对值添加到哈希表中
i = ne[i]
for i in range(len(list1)):
if i == len(list1) - 1:
print(f"{list1[i][0]:05d} {list1[i][1]} {-1}")
else:
print(f"{list1[i][0]:05d} {list1[i][1]} {list1[i + 1][0]:05d}")
for i in range(len(list2)):
if i == len(list2) - 1:
print(f"{list2[i][0]:05d} {list2[i][1]} {-1}")
else:
print(f"{list2[i][0]:05d} {list2[i][1]} {list2[i + 1][0]:05d}")