import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param head ListNode类 
     * @return ListNode类
     */
    public ListNode delete(ListNode head, int val) {
        while(head != null && head.val == val) {
            head = head.next;
        }
        return head;
    }
    public ListNode deleteHead(ListNode head) {
        if(head == null) {
            return null;
        }
        ListNode pre = head;
        ListNode cur = head.next;
        while(cur != null && pre.val == cur.val) {
            head = delete(head, pre.val);
            if(head == null) {
                return null;
            }
            pre = head;
            cur = head.next;
        }
        return head;
    }
    public ListNode deleteDuplicates (ListNode head) {
        head = deleteHead(head);
        if(head == null) {
            return null;
        }
        ListNode pre = head;
        ListNode cur = head.next;
        while(true) {
            cur = deleteHead(cur);
            pre.next = cur;
            if(cur == null) {
                break;
            }
            pre = cur;
            cur = cur.next;
        }
        return head;
    }
}