AcWing 826. 单链表--Python3
原题链接
简单
作者:
崔金浩Felix
,
2020-08-05 00:49:08
,
所有人可见
,
阅读 551
import sys
class LinkList(object):
def __init__(self):
super().__init__()
self.head = -1
self.idx = 1
self.e = [0] * 100010
self.ne = [0] * 100010
def insert(self, *args):
if len(args) == 1:
x = args[0]
self.e[self.idx] = x
self.ne[self.idx] = self.head
self.head = self.idx
self.idx += 1
else:
k, x = args
self.e[self.idx] = x
self.ne[self.idx] = self.ne[k]
self.ne[k] = self.idx
self.idx += 1
def remove(self,k):
if k == 0 and self.head != -1:
self.head = self.ne[self.head]
elif k != 0 and self.ne[k] != -1:
self.ne[k] = self.ne[self.ne[k]]
def traverse(self):
i = self.head
ret = []
while(i != -1):
ret.append(self.e[i])
i = self.ne[i]
return ret
if __name__ == '__main__':
m = int(sys.stdin.readline())
ll = LinkList()
for _ in range(m):
op, *args = list(sys.stdin.readline().strip().split())
args = list(map(int, args))
if op == 'H' or op == 'I':
ll.insert(*args)
elif op == 'D':
ll.remove(*args)
print(" ".join(map(str, ll.traverse())))