AcWing 826. 单链表
原题链接
简单
作者:
弈剑观星
,
2021-05-08 11:45:24
,
所有人可见
,
阅读 219
python3单链表类实现
class SingleLinkedList:
def __init__(self, length=100010):
self.length = length
self.val = [0] * self.length
self.next = [0] * self.length
self.head = -1
self.cur = 1
def add_to_head(self, x):
self.val[self.cur] = x
self.next[self.cur] = self.head
self.head = self.cur
self.cur += 1
def add(self, k, x):
self.val[self.cur] = x
self.next[self.cur] = self.next[k]
self.next[k] = self.cur
self.cur += 1
def delete(self, k):
if k == 0:
self.head = self.next[self.head]
return
self.next[k] = self.next[self.next[k]]
# 输入读取与处理
linked = SingleLinkedList()
m = int(input())
for _ in range(m):
s, *p = input().split()
if s == 'I':
k, x = map(int, p)
linked.add(k, x)
elif s == 'H':
x = int(p[0])
linked.add_to_head(x)
else:
k = int(p[0])
linked.delete(k)
tail = linked.head
while tail != -1:
print(linked.val[tail], end=' ')
tail = linked.next[tail]