leetcode--083--deleting repeating elements in a sorted list

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.