My Python Submission,brief although not short


  • 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
    """
    if head == None or head.next == None:
    return head
    while head.next and head.val == head.next.val:
    while head.next and head.val == head.next.val:
    head = head.next
    head = head.next
    if head == None:
    return head
    #the part above is too long , I hope I knew how to shorten it
    p = head
    if head == None or head.next == None:
    return head
    while p.next and p.next.next:
    if p.val != p.next.val and p.next.val != p.next.next.val:
    p = p.next
    else:
    q = p.next
    while q.next and q.val == q.next.val:
    q = q.next
    p.next = q.next
    return head


  • 0
    S

    #the part above is too long , I hope I knew how to shorten it


Log in to reply
 

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