Leetcode (82)-delete duplicate elements in a sorted list II

Source: Internet
Author: User

Given a sorted list, delete all nodes that contain duplicate numbers, leaving only the numbers in the original linked list that do not recur.

Example 1:

Input: 1->2->3->3->4->4->5 output: 1->2->5

Example 2:

Input: 1->1->1->2->3 output: 2->3

Idea: This topic is not the same as the previous one, because it does not leave repeating elements. My first thought was to traverse the list, calculate and save the number of each value, iterate through the list again, and leave the node that only appeared once, but it's not wise to traverse two of times, and the way I count is not so good, num[p->val]++, if the value in the list is negative, Obviously, there are other methods of counting here (not discussed here), but this is the first thing to give up.

The following direct code, I here is the point to note: (1) changes in the head node, if the repetition of the number appears in the beginning, the head node must be replaced (2) if the entire list has not found no duplicate nodes, meaning to return null, Here is the final note (3) Treatment of the end of the list, if the remaining one is not duplicated, to join the list, because the program's double pointer traversal, the condition is not judged

listnode* Deleteduplicates (listnode*head) {        if(Head==null | | head->next==null)returnHead; ListNode* p1,*p2,*P3; P3=NULL; P1=Head; P2=p1->Next; intnum=1, flag=0;  while(p2) {if(p2->val==p1->val) {num++; }            Else if(P2->val!=p1->val && num>1) {P1=P2; Num=1; }            Else            {                if(flag==0) {Head=p3=P1; Flag=1; }                Else{P3->next=P1; P3=p3->Next; } p1=p1->Next; } P2=p2->Next; }        if(p1->next==NULL) {            if(p3==NULL) {Head=p3=P1; }            Else{P3->next=P1; P3=p3->Next; }        }        if(P3==null)returnNULL; P3->next=NULL; returnHead; }

I think my above practice, although clear the idea, but still too cumbersome, so look for the great God of the simple code implementation

listnode* Deleteduplicates (listnode*head) {        if(!head)returnnullptr; ListNode* pre=nullptr; ListNode* cur=Head;  while(cur) {if(Cur->next && cur->next->val==cur->val) {                intA=cur->Val;  while(Cur->next && cur->next->val==a) {cur=cur->Next; }                if(!pre) {Head=cur->Next; }                Else{Pre->next=cur->Next; }            }            Else{Pre=cur; } cur=cur->Next; }        returnHead; }

Leetcode (82)-delete duplicate elements in a sorted list II

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.