首页 > 编程语言 > 详细

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

时间:2020-12-20 20:36:34      阅读:31      评论:0      收藏:0      [点我收藏+]

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

Difficulty: 中等

给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 _没有重复出现 _的数字。

示例 1:

输入: 1->2->3->3->4->4->5
输出: 1->2->5

示例 2:

输入: 1->1->1->2->3
输出: 2->3

Solution

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
?
class Solution:
    def deleteDuplicates(self, head: ListNode) -> ListNode:
        if not head: return None
        res = ListNode(-1)
        res.next = head
        pre = res
        
        while head and head.next:
            if head.val == head.next.val:
                while head and head.next and head.val == head.next.val:
                    head = head.next
                head = head.next
                pre.next = head
            else:
                pre = pre.next
                head = head.next
        return res.next

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

原文:https://www.cnblogs.com/swordspoet/p/14164341.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!