题解 | #【模板】链表#
【模板】链表
https://www.nowcoder.com/practice/97dc1ac2311046618fd19960041e3c6f
class node:
def __init__(self, data=None, next=None):
self.data = data
self.next = next
class link:
def __init__(self):
self.head = node()
def insert(self, x, y):
a = self.head
while True:
b = a.next
if b is None:
a.next = node(y)
return None
elif b.data == x:
c = node(y)
a.next = c
c.next = b
return None
a = b
def delete(self, x):
a = self.head
while True:
b = a.next
if b is None:
return None
elif b.data == x:
c = b.next
a.next = c
return None
a = b
def list(self):
a = self.head
b = []
while True:
a = a.next
if a is None:
return b
b.append(a.data)
n = int(input())
a = link()
for i in range(n):
s = input().split()
if s[0] == 'insert':
x, y = int(s[1]), int(s[2])
#print(x,y)
a.insert(x, y)
else:
x = int(s[1])
a.delete(x)
res = a.list()
if len(res) == 0:
print('NULL')
else:
print(*res)

查看10道真题和解析