【LeetCode】82.删除排序链表中的重复元素 II

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time: 2019/3/23
# @Author: xfLi
# The file...

"""
问题分析:
(1)当遇到重复的节点,循环next到最后一个重复节点
(2)对重复节点做标记
"""

class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

def deleteDuplicates(head):
    if not head:
        return
    cur = head
    h = ListNode(0)
    h.next = head
    pre = h
    while cur:
        flag = False
        while cur.next and cur.val == cur.next.val:
            cur = cur.next
            flag = True
        if flag is False:
            pre = cur
        else:
            pre.next = cur.next
        cur = cur.next
    return h.next

猜你喜欢

转载自blog.csdn.net/qq_30159015/article/details/88774607