思路:

该题的关键是可能第一个节点就是重复的,遍历时需要把这种情况也考虑进去。

自测案例是{1,1,2}

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 *   public ListNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param head ListNode类 
     * @return ListNode类
     */
    public ListNode deleteDuplicates (ListNode head) {
        // write code here
        if (head == null) return null;
        ListNode dummyNode = new ListNode(-1);
        dummyNode.next = head;

        // 可能存在第一个数就是重复的情况
        ListNode cur = dummyNode;
        while (cur.next != null && cur.next.next != null) {
            if (cur.next.val == cur.next.next.val) {
                // 跳过相同的直到遇到不相同的
                int tmp = cur.next.val;
                while(cur.next != null && cur.next.val == tmp) {
                    cur.next = cur.next.next;
                }
            }
            else {
                cur = cur.next;
            }
        }
        return dummyNode.next;
    }
}