Page tree
Skip to end of metadata
Go to start of metadata

Runtime: 6 ms, faster than 5.07% of Java online submissions for Remove Duplicates from Sorted List II.
Memory Usage: 38.3 MB, less than 56.57% of Java online submissions for Remove Duplicates from Sorted List II.

	public class Solution {
        public ListNode deleteDuplicates(ListNode head) {

            Map<Integer, Integer> map = new TreeMap<>();

            ListNode current = head;
            while (Objects.nonNull(current)) {
                map.put(current.val, map.getOrDefault(current.val, 0) + 1);
                current = current.next;
            }

            ListNode result = new ListNode(0);
            ListNode tail = result;

            for (final Map.Entry<Integer, Integer> entry : map.entrySet()) {
                if (entry.getValue() == 1) {
                    tail.next = new ListNode(entry.getKey());
                    tail = tail.next;
                }
            }

            return result.next;
        }
    }

TreeMap 을 활용해 순서 보장!!

getOrDefault 내장 API를 사용해 해결

  • No labels