python - delete dup - simple


  • 0
    S
    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution(object):
        def deleteDuplicates(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            l1, l2 = head, head.next if head else None
            
            # null list
            # singleton list
            # key on edges
            while l2:
                if l1 and l1.val == l2.val:
                    l1.next = l2.next
                else:
                    l1 = l2
                l2 = l2.next
            
            return head
    

Log in to reply
 

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