Skip to content

Latest commit

 

History

History
49 lines (38 loc) · 944 Bytes

82.md

File metadata and controls

49 lines (38 loc) · 944 Bytes

Remove Duplicates from Sorted List II

Description

link


Solution

See Code

注意边界问题


Code

O(n)

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if not head:
            return None
        cur = head
        pre = res = ListNode(0)
        pre.next = head
        while cur and cur.next:
            if cur.next.val != cur.val:
                pre = cur
                cur = cur.next
            else:
                while cur.next and cur.val == cur.next.val:
                    cur = cur.next
                pre.next = cur.next
                cur = cur.next
        return res.next