题解 | #【模板】链表#
【模板】链表
http://www.nowcoder.com/practice/97dc1ac2311046618fd19960041e3c6f
class linkedlist: def __init__(self): self.items = [] def insert(self,x,y): if x in self.items: loc = self.items.index(x) self.items.insert(loc,y) else: self.items.append(y) def delete(self,x): if x in self.items: loc = self.items.index(x) self.items.pop(loc) else: print() def size(self): return len(self.items) def taverlse(self): for i in range(len(self.items)): print(self.items[i], end = " ") s = linkedlist() n = int(input()) for i in range(n): m = input() case = m.split(" ")[0] if case == "insert": idx = int(m.split(" ")[1]) num = int(m.split(" ")[2]) s.insert(idx,num) elif case == "delete" and not s.size() == 0: idx = int(m.split(" ")[1]) s.delete(idx) if s.size() == 0: print("NULL") else: s.taverlse()