思路
code
public ListNode deleteDuplicates(ListNode head) {
//虚拟节点的next指向head 快指针指向head 慢指针指向dummy
ListNode dummy = new ListNode(0),fast =head,slow = dummy;
slow.next = fast; //快慢指针连在一起
while(fast!=null){ //一直到快指针到头
while(fast.next!=null&&fast.val==fast.next.val){
fast = fast.next; //如果值重复 则将fast向后
}
if(slow.next!=fast){ //如果此时没有重复元素,则slow.next=fast
slow.next = fast.next; //将slow跳到fast
fast = fast.next; //将fast向后移动
}else{
slow = slow.next; //如果没有则都相互移动
fast = fast.next;
}
}
return dummy.next;
}