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
}
}