class Node(): def __init__(self, key): self.key = key self.next = None class LinkedList(): def __init__(self, key): node = Node(key) # 初始化时插入第一个元素 self.head = node def search(self, key): x = self.head x_pre = None # x前面一个节点,用于delete操作 while x is not None and x.key != key: x_pre = x x = x.next return x, x_pre def insert(self, key, pre_key): node = Node(key) x, x_pre = self.search(pre_key) # 查找key等于pre_key的元素x,找到后在x后面插入新node if x is not None: node.next = x.next x.next = node def delete(self, key): # 两种情况:删除头元素和删除中间元素 x, x_pre = self.search(key) if x is not None: # 存在需要被删除的元素 if x_pre is not None: # 如果不是头元素 x_pre.next = x.next else: self.head = x.next def print_list(self): x = self.head while x is not None: print(x.key, end=' ') x = x.next print() while True: try: a = list(map(int, input().split())) my_list = LinkedList(a[1]) for i in range(2, a[0] * 2, 2): # 取a[2:-1]之间的元素,步长为2 my_list.insert(a[i], a[i + 1]) my_list.delete(a[-1]) my_list.print_list() except: break