题解 | #从单向链表中删除指定值的节点#
从单向链表中删除指定值的节点
https://www.nowcoder.com/practice/f96cd47e812842269058d483a11ced4f
[TOC]
python3标准链表解法
如果第一次接触可以参考大佬视频,很详细,点击图片传送 |
#建立链表节点模型 class Node(): def __init__(self,item): self.item = item self.next = None #创建链表类 class LinkList(): #定义头节点 def __init__(self): self.head=None #定义增加函数 def insert_node(self,n,m): curNode = self.head node = Node(n) while curNode: if curNode.item == m: node.next=curNode.next curNode.next=node break else: curNode = curNode.next #定义删除函数 def remove_node(self,data): curNode = self.head preNode = None while curNode: if curNode.item == data: if not preNode: self.head = curNode.next break else: preNode.next = curNode.next break else: preNode = curNode curNode = curNode.next #定义遍历打印链表函数 def print_Node(self): curNode = self.head while curNode: print(curNode.item,end=' ') curNode = curNode.next while True: try: lst = [int(i) for i in input().split()] l = LinkList() l.head = Node(lst[1]) rm = lst[-1] for i in range(0,len(lst[2:-1]),2): n = lst[2:-1][i] m = lst[2:-1][i+1] l.insert_node(n, m) l.remove_node(rm) l.print_Node() except: break#数据结构编程链表#