https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/


Runtime: 7 ms, faster than 5.15% of Java online submissions for Remove Duplicates from Sorted List II.
Memory Usage: 38.4 MB, less than 49.33% of Java online submissions for Remove Duplicates from Sorted List II.


```java
import java.util.ArrayList;
import java.util.List;
import java.lang.Math;
import java.util.Collections;

public class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}

class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        //7:20
        if (head == null) {
            return null;
        }

        List<ListNode> node_list = new ArrayList<ListNode>();
        List<Integer> value_list = new ArrayList<Integer>();
        List<Integer> duplicates = new ArrayList<Integer>();

        ListNode node = head;
        while (node != null) { 
            node_list.add(node);
            if (value_list.contains(Integer.valueOf(node.val))) {
                duplicates.add(node.val);
            } else {
                value_list.add(node.val);
            }
            node = node.next;
        }

        List<ListNode> new_node_list = new ArrayList<ListNode>();
        for (ListNode a_node : node_list) {
            if (!duplicates.contains(a_node.val)) {
                new_node_list.add(a_node);
            }
        }

        for (int i=0; i < new_node_list.size(); i++) {
            if (i != new_node_list.size() -1) {
                new_node_list.get(i).next = new_node_list.get(i+1);
            } else {
                new_node_list.get(i).next = null;
            }
        }

        if (new_node_list.size() == 0) {
            return null;
        } else {
            return new_node_list.get(0);
        }
        //7:42
        //the stupid java has to use get(i) to get an element from list
    }
}
```