Skip to the content.

leetcode [82] 删除排序链表中的重复元素 II


Contact me:

Blog -> https://cugtyt.github.io/blog/index
Email -> cugtyt@qq.com
GitHub -> Cugtyt@GitHub


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

示例 1:

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

示例 2:

输入: 1->1->1->2->3
输出: 2->3
class Solution:
    def deleteDuplicates(self, head: ListNode) -> ListNode:
        head_ = ListNode(0)
        head_.next = head
        head_next = head
        res = head_
        while head_ and head_next and head_next.next:
            if head_next.val == head_next.next.val:
                while head_next and head_next.next and head_next.val == head_next.next.val:
                    head_next = head_next.next
                head_.next = head_next.next
                head_next = head_.next
                head = head_next
            else:
                head_ = head_next
                head_next = head_.next
        return res.next