Skip to content

LC 0082 [M] Remove Duplicates from Sorted List II

Code with Senpai edited this page May 20, 2022 · 1 revision
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def deleteDuplicates(self, head):
        dum = pre = ListNode(0)
        dum.next = head
        cur = head 
        while cur and cur.next:
            if cur.val == cur.next.val:
                while cur and cur.next and cur.val == cur.next.val:
                    cur = cur.next
                cur = cur.next
                pre.next = cur
            else:
                pre = pre.next
                cur = cur.next
        return dum.next
Clone this wiki locally