Share my python solution, using two pointers

  • 0
    class Solution:
        # @param {ListNode} head
        # @return {ListNode}
        def deleteDuplicates(self, head):
            if head is None:
                return None
            if is None:
                return head
            p1, p2 = head,
            flag = False
            while p2 is not None:
                if p1.val == p2.val:
                    p1, p2 = self.utilRemoveDuplicates(p1, p2)
                    p1 = p2
                    p2 =
            return head
        def utilRemoveDuplicates(self, p1, p2):
            while hasattr(p1, "val") and hasattr(p2, "val") and (p1.val == p2.val) and (p2 is not None):
                p2 =
            if p2 is None:
       = None
       = p2
                p1 = p2
                p2 =
            return p1, p2

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.