题解 | #删除有序链表中重复的元素-II#
删除有序链表中重复的元素-II
http://www.nowcoder.com/practice/71cef9f8b5564579bf7ed93fbe0b2024
😔😔😔
import java.util.*;
public class Solution {
public ListNode deleteDuplicates (ListNode head) {
if(head==null||head.next==null)
return head;
ListNode newHead = new ListNode(0);
ListNode pre = newHead;
HashMap<Integer,Integer> hm = new LinkedHashMap<>();
while(head!=null)
{
if(hm.containsKey(head.val))
hm.put(head.val,hm.get(head.val)+1);
else
hm.put(head.val,1);
head = head.next;
}
for(Integer keys : hm.keySet()){
if(hm.get(keys)==1){
pre.next = new ListNode(keys);
pre = pre.next;
}
}
pre = null;
return newHead.next;
}
}
