Delete duplicate nodes in a linked list java__ delete duplicate points

Source: Internet
Author: User

Ideas: Define four nodes, the front node prenode, the current node node, the next node NextNode, delete node Delnode, in the face of the deletion node to ensure that the Prenode connection nextnode to prevent fracture.

public static void Deleteduplication (Listnote root) {if (root==null) {return; Listnote prenode=null;//Front node Listnote node=root;//current node while (node!=null) {listnote NE
Xtnode=node.getnext ();//Next node Boolean needdelete=false; 
          Determine whether the current node and the next node value are equal if (Nextnode!=null&&nextnode.getvalue () ==node.getvalue ()) needdelete=true;
              if (!needdelete) {//unequal, move forward prenode=node;            
          Node=node.getnext ();
              else{//equal, delete the node int value=node.getvalue ();
              Listnote Tobedel=node;
                  while (Tobedel!=null&&tobedel.getvalue () ==value) {nextnode=tobedel.getnext ();//Delete the node
              Tobedel=nextnode;
              } if (Prenode==null) {//Header node deleted when Root=nextnode; else{//That is, the duplicate node Prenode.setnext is deleted (NEXTnode);
          } Node=nextnode; }
        }
    }

Define one-way linked list Listnote

public class Listnote {

    private  listnote nextnote;
    private int value;
    Public Listnote () {  
    } public
    listnote (int value) {
        this.value=value;
    }  
    Public  listnote GetNext () {return     
        nextnote;            
    }
    public void Setnext (Listnote next) {this
        . Nextnote=next; 
    }   
    public int GetValue () {return
        value;   
    }
    public void SetValue (int value) {
        this.value=value
    }
}

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.