import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * } */ public class Solution { /** * * @param head ListNode类 * @return ListNode类 */ public ListNode deleteDuplicates (ListNode head) { // write code here if (head == null || head.next == null) { return head; } Map<Integer, Integer> map = new HashMap<>(); ListNode cur = head; while (cur != null) { map.put(cur.val, map.getOrDefault(cur.val, 0) + 1); cur = cur.next; } ListNode tail = new ListNode(0); ListNode newHead = null; cur = head; while (cur != null) { if (map.get(cur.val) == 1) { if (newHead == null) { newHead = cur; } tail.next = cur; tail = cur; } cur = cur.next; } tail.next = null; return newHead; } }