public static ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }

        ListNode safeNode = new ListNode(Integer.MIN_VALUE);
        safeNode.next = head;

        ListNode pre, cur, post;
        pre = safeNode;
        cur = pre.next;

        while (cur != null) {
            if (cur.next == null) {
                break;
            }

            post = cur.next;
            boolean flag = false;

            while (post != null && post.val == cur.val) {
                flag = true;
                post = post.next;
            }

            if (flag) {
                pre.next = post;
                cur = post;
                if (cur == null) {
                    break;
                }
                post = cur.next;
            } else {
                pre = cur;
                cur = cur.next;
            }
        }

        return safeNode.next;
    }