/* public class ListNode { int val; ListNode next = null;
ListNode(int val) {
this.val = val;
}
} */
import java.util.*;
public class Solution {
public ListNode deleteDuplication(ListNode pHead) {
//先使用散列表HashMap记录无重复数字的节点
//以{1,2,2,3,3,4}为例,得到{1:true, 2:false, 3:false, 4:true}
HashMap<Integer, Boolean> map = new HashMap<>();
ListNode cur = pHead;
while(cur != null){
if(map.containsKey(cur.val)){
map.put(cur.val, false);
}else{
map.put(cur.val, true);
}
cur = cur.next;
}
//设置遍历指针cur初始位于pHead, 以及记录指针tmp
//设置头节点head,记录初始tmp指针的位置,(同时是为了防止初始pHead.val为重复数值出错)
//head.next = pHead;
cur = pHead;
ListNode tmp = new ListNode(-1);
tmp.next = pHead;
ListNode head = tmp;
while(cur != null){
if(map.get(cur.val).equals(true)){
tmp.next = cur;
tmp = cur;
cur = cur.next;
}else{
cur = cur.next;
}
}
//一下是针对{1,1}出错所做的修改
tmp.next = cur;
tmp = cur;
return head.next;
}
}