题目:
分析:
设置哑节点,以pre和now的形式依次遍历下去即可
代码:
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head==null || head.next==null)
return head;
ListNode none=new ListNode(Integer.MAX_VALUE-2);
ListNode pre=none;
ListNode now=head;
none.next=head;
while(now!=null){
if(pre.val==now.val) {
pre.next = now.next;
now=now.next;
continue;
}
pre=pre.next;
now=now.next;
}
return none.next;
}
}
结果: