Remove duplicates from an unsorted linked list

前端 未结 19 973
梦如初夏
梦如初夏 2020-12-09 06:54
import java.util.*;
/*
 *  Remove duplicates from an unsorted linked list
 */
public class LinkedListNode {  
    public int data;  
    public LinkedListNode next;          


        
19条回答
  •  有刺的猬
    2020-12-09 07:25

    Ans is in C . first sorted link list sort() in nlog time and then deleted duplicate del_dip() .

    node * partition(node *start)
    {
        node *l1=start;
        node *temp1=NULL;
        node *temp2=NULL;
        if(start->next==NULL)
            return start;
    
        node * l2=f_b_split(start);
          if(l1->next!=NULL)
            temp1=partition(l1);
          if(l2->next!=NULL)
                temp2=partition(l2);
    
    if(temp1==NULL || temp2==NULL)
        {
            if(temp1==NULL && temp2==NULL)
            temp1=s_m(l1,l2);
    
            else if(temp1==NULL)
            temp1=s_m(l1,temp2);
    
            else if(temp2==NULL)
            temp1=s_m(temp1,l2);
    }
        else
                temp1=s_m(temp1,temp2);
    
        return temp1;
    }
    
    node * sort(node * start)
    {
        node * temp=partition(start);
            return temp;
    }
    
    void del_dup(node * start)
    {
      node * temp;
        start=sort(start);
        while(start!=NULL)
            {
            if(start->next!=NULL && start->data==start->next->data  )
                {
                    temp=start->next;
                    start->next=start->next->next;
                    free(temp);
                continue;
                }
            start=start->next;
            }
    }
    
    void main()
     {
        del_dup(list1);
        print(list1);
    } 
    

提交回复
热议问题