用栈的方法:

public ListNode deleteDuplicates (ListNode head) {
        // write code here
        Stack<ListNode> stack = new Stack<>();
        if (head == null || head.next == null) {
            return head;
        }
        ListNode result = head;
        ListNode temp = head.next;
        stack.push(head);
        while (!stack.isEmpty()) {
            if (stack.peek().val == temp.val) {
                stack.peek().next = temp.next;
            } else {
                stack.push(temp);
            }
            if (temp.next != null) {
                temp = temp.next;
            } else {
                break;
            }
        }
          return result;  
    }