Python Recursive Solution with comments, 56ms

  • 0
    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    # = None
    class Solution(object):
        def deleteDuplicates(self, head):
            :type head: ListNode
            :rtype: ListNode
            # zero or only one node
            if head == None or == None:
                return head
            # if not unique
            if head.val ==
                # discard the second nodes with the same value
                while and head.val ==
                # also discard the first node
                return self.deleteDuplicates(
            # if unique
                # keep the head node
       = self.deleteDuplicates(
                return head

Log in to reply

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