package 递增链表去重;
public abstract class ListTemple {
publicabstract void cutRepeat01();
publicabstract void cutRepeat02();
publiclong getTime1(){
longs=System.currentTimeMillis();
cutRepeat01();
longe=System.currentTimeMillis();
returne-s;
}
publiclong getTime2(){
longs=System.currentTimeMillis();
cutRepeat02();
longe=System.currentTimeMillis();
returne-s;
}
publicabstract void lastInsert(int i);
publicabstract void display();
}
package 递增链表去重;
public class Node {
publicint data;
publicNode next;
publicNode(){
this.data=0;
this.next=null;
}
publicNode(int data){
this.data=data;
this.next=null;
}
}
package 递增链表去重;
public class LinkedListDemo extendsListTemple{
publicNode head = null;
//创建头指针
publicLinkedListDemo() {
head= new Node();
}
//插入
publicvoid lastInsert(int data) {
Nodenode = new Node(data);
Nodep = head;
if(p.next == null) {
head.next= node;
}else {
while(p.next != null) {
p= p.next;
}
}
p.next= node;
}
//输出
publicvoid display() {
Nodenode = head.next;
while(node != null) {
System.out.print(node.data+ " ");
node= node.next;
}
}
//去重复
publicvoid cutRepeat01() {
Nodep = head.next;
while(p.next != null) {
if(p.data == p.next.data) {
p.next= p.next.next;
}else {
p= p.next;
}
}
}
//删除头节点
publicvoid deleteFirst() {
Nodep = head.next;
Noder = p;
p= p.next;
r= null;
System.gc();
head.next= p;
}
//去重复方法2
publicvoid cutRepeat02() {
Nodep = head.next;
Nodeq = p.next;
Noder = null;
while(q != null) {
while(q!=null && p.data==q.data) {
q=q.next;
}
if(q!=null){
p=p.next;
p.data=q.data;
}
}
q=p.next;
p.next=null;
while(q!=null){
r=q;
q=q.next;
r=null;
System.gc();
}
}
}
package 递增链表去重;
public class TestMain {
publicstatic void main(String[] args) {
ListTemplelist = new LinkedListDemo();
//插入
for(int i = 1; i <= 100; ++i) {
list.lastInsert(i);
}
//去重复
longt1 = list.getTime1();
longt2 = list.getTime2();
System.out.println(t1);
System.out.println(t2);
//输出
//list.display();
}
}