题解 | #删除有序链表中重复的元素-II#
删除有序链表中重复的元素-II
https://www.nowcoder.com/practice/71cef9f8b5564579bf7ed93fbe0b2024
import java.util.*;
public class Solution {
public ListNode deleteDuplicates (ListNode head) {
ListNode node=head;
HashMap<Integer,Integer> map=new HashMap<>();
while(head!=null){
if(map.containsKey(head.val)){
map.put(head.val,map.get(head.val)+1);
}else{
map.put(head.val,1);
}
head=head.next;
}
ListNode list=new ListNode(-1);
list.next=node;
ListNode cur=list;
while(node!=null){
if(map.get(node.val)>1){
list.next=list.next.next;
}else{
list=list.next;
}
node=node.next;
}
return cur.next;
}
}
查看12道真题和解析
360集团公司福利 410人发布