来自:https://blog.csdn.net/futureflyme/article/details/72780789
package interview;
import java.util.*;
//定义节点结构
class Node {
int data;
Node next = null;
Node(int data) {
this.data = data;
}
}
public class TestDuplicate {
public static void main(String[] args) {
int [] arr=new int[] {5,4,4,3,3,2,1};
//定义一个for循环,每次在链表头部插入元素,从而创建一个链表
Node head1=null;
for(int i=0;i<arr.length;i++){
Node node=new Node(arr[i]);
node.next=head1;
head1=node;
}
System.out.println("原来的链表: ");
Node temp1=head1;
while(temp1!=null){
System.out.print(temp1.data+" ");
temp1=temp1.next;
}
System.out.println();
Node head2 = deleteDuplication(head1);
System.out.println("去重后的链表: ");
Node temp2=head2;
while(temp2!=null){
System.out.print(temp2.data+" ");
temp2=temp2.next;
}
}
//这个方法不是真正的链表去重
public static Node deleteDuplication(Node phead) {
HashSet<Integer> set = new HashSet<Integer>();
Node tempNode = phead;
while (tempNode != null) {
set.add(tempNode.data);
tempNode = tempNode.next;
}
//for循环,每次在链表的尾部插入元素,从而创建一个链表
Node head=null;
Node temp = head;
for (Integer num : set) {
Node node = new Node(num);
if(head==null){
head=node;
}else{
temp.next=node;
}
temp=node;
}
return head;
}
}