Problem Description:
Given a sorted list, delete all duplicate elements so that each element appears only once.
Example 1:
input: 1->1->2 output: 1->2
Example 2:
input: 1->1->2->3->3 output: 1->2->3
Method 1:(timeout)
1 classsolution (object):2 defdeleteduplicates (Self, head):3 """4 : Type Head:listnode5 : Rtype:listnode6 """7 8p =Head9 ifp = = NoneorP.next = =None:Ten returnHead One whileP.next! =None: AQ =P.next - ifP.val = =Q.val: -Q =Q.next the Else: -P.next =Q -p =Q - returnHead
Method 2:
1 classsolution (object):2 defdeleteduplicates (Self, head):3 """4 : Type Head:listnode5 : Rtype:listnode6 """7 8p =Head9 ifp = = NoneorP.next = =None:Ten returnHead One whileP.next! =None: AQ =P.next - ifP.val = =Q.val: -P.next =Q.next the Else: -p =P.next - returnHead
Ditto:
1 classsolution (object):2 defdeleteduplicates (Self, head):3 """4 : Type Head:listnode5 : Rtype:listnode6 """7 #This is a list of non-lead nodes8 ifHead isNone:#The linked list is empty9 returnHeadTenCur=Head One whileCur.next:#next node is not empty A ifCur.val==cur.next.val:#For the first time, the head element is equal to the value of the next node of the head element ... -cur.next=Cur.next.next - Else: theCur=Cur.next - returnHead
Method 2:
1 classsolution (object):2 defdeleteduplicates (Self, head):3 """4 : Type Head:listnode5 : Rtype:listnode6 """7A=[]8L=Head9 whileL:Ten ifL.valinchA: Onep.next=L.next A Else: - a.append (l.val) -p=L theL=L.next - returnHead
2018-07-25 13:08:38
leetcode--083--deleting repeating elements in a sorted list