小黑独自咖啡厅享受思考心流:82. 删除排序链表中的重复元素 II
小黑代码
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:# 0个或者一个节点的情况if not (head and head.next):return head# 辅助结点add_node = ListNode()add_node.next = head# 前序指针pre_node = add_nodecur_node = head# 开始遍历while cur_node.next:# 出现相等的情况则删除if cur_node.val == cur_node.next.val:value = cur_node.valcur_node = cur_node.next.next# 去掉该值的结点while cur_node and cur_node.val == value:cur_node = cur_node.nextpre_node.next = cur_nodeif not cur_node:breakelse:cur_node = cur_node.nextpre_node = pre_node.nextreturn add_node.next