Lintcode 112. Delete a repeating element in a sorted list

Source: Internet
Author: User
Tags lintcode

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

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

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

/** Definition of ListNode * class ListNode {* Public: * int val; * ListNode *next; * ListNode (int val) { * This->val = val; * This->next = NULL; *     } * } */classSolution { Public:    /** @param Head:head is the head of the linked list * @return: Head of linked list*/ListNode* Deleteduplicates (ListNode *head) {        //Write your code here        if(head==NULL)return 0; ListNode*p=Head; ListNode*Next;  while(p->next!=NULL) {Next=p->Next; if(next->val==p->val) {P->next=next->Next; Delete(next); }Else//else is important .{p=p->Next; }                    }        returnHead; }};

Lintcode 112. Delete a repeating element 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.