/** * 设置一个首节点,遍历扫描,并设置 * pre,cur,next三个指针 * * @param head * @return */ public static ListNode deleteDuplicates(ListNode head) { // write code here ListNode node = new ListNode(0); node.next = head; ListNode next; ListNode cur = head; ListNode pre = node; while (cur != null) { next = cur.next; boolean tmp = false; while (next != null && cur.val == next.val) { next = next.next; pre.next = next; // 前一个不同节点指向下一个不同节点 tmp = true; } if (!tmp) { // 如果以前没有相同的节点,则pre指向当前节点,保证下次遇到相同的节点设置next pre = cur; } cur = next; } return node.next; }