Lintcode Python Simple class topic 112. Delete duplicate elements in a linked list

Source: Internet
Author: User
Tags lintcode

Title Description:

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

Have you ever encountered this problem in a real interview? Yes Sample Example

Give 1->1->2->null , return1->2->null

Give 1->1->2->3->3->null , return1->2->3->null

labelLinked list

Topic Analysis:

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

Source:

"" "Definition of Listnodeclass ListNode (object):    def __init__ (self, Val, next=none):        self.val = val        Self.next = Next "" "Class Solution:" "    @param head:a listnode    @return: A listnode"    "" Def Deleteduplicates (self, head):        # Write your code this        if head is none:            return None        pre = head        cur = Pre        fol = Pre.next        while fol.next are not None:            # always find the first fol that is different from the pre value, delete all the repeating elements in the middle            if pre.val = = Fol.val :                fol = fol.next            else:                pre.next = fol                pre = fol                fol = pre.next        # Detect if the end is equal        if Pre.val = = Fol.val:            pre.next = fol.next        else:            pre.next = fol        return cur

  

Lintcode Python Simple class topic 112. Delete duplicate elements in a linked list

Related Article

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.