Delete a repeating element in a sorted list (simple)

Source: Internet
Author: User

This problem is relatively simple, do not do too much description

Given a sorted list, delete all duplicate elements, leaving only one for each element.

example

give  1->1->2->null , return  1->2->null

give  1->1->2->3->3->null , return  < Code style= "PADDING:2PX 4px; Color: #c7254e; Font-family:menlo, Monaco, Consolas, "courier New", monospace; font-size:12.6px; White-space:nowrap; Background-color: #f9f2f4 ">1->2->3->null

"" "Definition of Listnodeclass ListNode (object):    def __init__ (self, Val, next=none):        self.val = val        Self.next = Next "" "Class Solution:" "    @param: Head:head is the head of the linked list    @return: Head of link Ed list    "" "    def deleteduplicates (self, head):        If head was None:            return head        temp = head        while Temp.next is not None:            if Temp.next.val = = Temp.val:                temp.next = temp.next.next            else:                temp = Temp.next        return head

  

Delete a repeating element in a sorted list (simple)

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.