class Node {
public int data;
public Node next;
public Node ( int data) {
this . data = data;
}
}
public class SingleList {
public Node head;
public void addFirst ( int data) {
Node node = new Node ( data) ;
if ( this . head == null) {
this . head = node;
return ;
} else {
node. next = this . head;
this . head = node;
}
}
public void show ( ) {
Node cur= this . head;
while ( cur!= null) {
System. out. print ( cur. data+ " " ) ;
cur= cur. next;
}
System. out. println ( ) ;
}
public Node delDuplication ( ) {
Node newHead= new Node ( - 1 ) ;
Node cur= this . head;
Node tmp= newHead;
while ( cur!= null) {
if ( cur. next != null && cur. data == cur. next. data) {
while ( cur. next != null && cur. data == cur. next. data) {
cur = cur. next;
}
cur = cur. next;
}
else {
tmp. next = cur;
tmp = tmp. next;
cur = cur. next;
}
}
tmp. next= null;
return newHead. next;
}
}
public class TestDemo {
public static void main ( String[ ] args) {
SingleList singleList= new SingleList ( ) ;
singleList. addFirst ( 5 ) ;
singleList. addFirst ( 5 ) ;
singleList. addFirst ( 4 ) ;
singleList. addFirst ( 4 ) ;
singleList. addFirst ( 1 ) ;
singleList. show ( ) ;
singleList. delDuplication ( ) ;
singleList. show ( ) ;
}
}