其中链表为递增的
例子:1->2->2->3->3->4->5->6移除重复的元素之后为
1->4->5->6
/*
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){
if(pHead == null) return null;
HashMap<Integer,Integer> map = new HashMap<>();
ListNode cur = pHead;
while(cur != null){
map.put(cur.val,map.getOrDefault(cur.val,0) + 1);
cur = cur.next;
}
ListNode head = new ListNode(0);
head.next = pHead;
cur = pHead;
ListNode pre = head;
while(cur != null){
if(map.get(cur.val) != 1){
pre.next = cur.next;
cur = cur.next;
}else{
pre = cur;
cur = cur.next;
}
}
return head.next;
}
}