Skip to content

Latest commit

 

History

History
111 lines (95 loc) · 3.11 KB

82.md

File metadata and controls

111 lines (95 loc) · 3.11 KB

题目地址

https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/

解答1

class Solution:
    def deleteDuplicates(self, head: ListNode) -> ListNode:
        """
            删除排序链表中的重复元素 II(非最优解)

            给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。
            输入: 1->2->3->3->4->4->5
            输出: 1->2->5
        """
        if not head:
            return None
        # 先找到head
        i = head
        while i.next:
            j = i.next
            if j.val != i.val:
                break
            while j.next and i.val == j.val:
                j = j.next
            if i.val == j.val:
                return None
            i = j
        head = i
        # 再找next
        if i.next:
            k, i, t = i, j, False
            while i.next:
                j = i.next
                total = 0
                while j.next and i.val == j.val:
                    total += 1
                    j = j.next
                if not total and i.val != j.val:
                    k.next = i
                    k = i
                elif i.val == j.val:
                    t = True

                i = j

            k.next = i if not t else None

        return head

解答2

class Solution:
    def deleteDuplicates(self, head: ListNode) -> ListNode:
        """
            删除排序链表中的重复元素 II(非最优解)

            给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。
            输入: 1->2->3->3->4->4->5
            输出: 1->2->5
        """
        dummy = ListNode(0)
        pt = dummy
        repeated = 0
        while head and head.next:
            if head.val != head.next.val:
                if not repeated:
                    pt.next = head
                    pt = pt.next
                repeated = 0
            else:
                repeated = 1
            head = head.next

        pt.next = None if repeated else head

        return dummy.next

解答3

class Solution:
    def deleteDuplicates(self, head: ListNode) -> ListNode:
        """
            删除排序链表中的重复元素 II

            给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。
            输入: 1->2->3->3->4->4->5
            输出: 1->2->5
        """
        cur, newhead = head, None
        temp = newhead
        while cur:
            curnext = cur.next

            while curnext and curnext.val == cur.val:
                curnext = curnext.next
            if not newhead or newhead == cur:
                newhead = cur if cur.next == curnext else curnext
                temp = newhead
            else:
                temp.next = cur if cur.next == curnext and cur != temp else curnext

                if temp.next and temp.next.next and temp.next.val != temp.next.next.val:
                    temp = temp.next

            cur = curnext
        return newhead