JZ76 删除链表中重复的结点
删除链表中重复的结点_牛客题霸_牛客网 (nowcoder.com)
// import java.util.*;
// public class Solution {
// public ListNode deleteDuplication(ListNode head) {
// if(head==null){
// return null;
// }
// ListNode vhead=new ListNode(-1);
// vhead.next=head;
// ListNode cur=vhead;
// while(cur.next!=null&&cur.next.next!=null){
// if(cur.next.val==cur.next.next.val){
// int tmp=cur.next.val;
// while(cur.next!=null&&cur.next.val==tmp){
// cur.next=cur.next.next;
// }
// }else{
// cur=cur.next;
// }
// }
// return vhead.next;
// }
// }
//11
//map
import java.util.*;
public class Solution {
public ListNode deleteDuplication(ListNode head) {
if(head==null){
return null;
}
Map<Integer,Integer> map=new HashMap<>();
ListNode cur=head;
while(cur!=null){
if(map.containsKey(cur.val)){
map.put(cur.val,map.get(cur.val)+1);
}else{
map.put(cur.val,1);
}
cur=cur.next;
}
ListNode vhead=new ListNode(-1);
vhead.next=head;
cur=vhead;
while(cur.next!=null){
if(map.get(cur.next.val)!=1){
cur.next=cur.next.next;
}else{
cur=cur.next;
}
}
return vhead.next;
}
}