public ListNode deleteDuplication( ) {
ListNode newHead = new ListNode(-1);
ListNode tmp = newHead;
ListNode cur = head;
while (cur != null){
if (cur.next != null && cur.val == cur.next.val){
while (cur.next != null && cur.val == cur.next.val){
cur = cur.next;
}
cur = cur.next; //cur.next != null不进入循环,此时的cur还没指向尾节点,所以要走多一步
}else { //cur.next != null && cur.val != cur.next.val
tmp.next = cur;
tmp = tmp.next;
cur = cur.next;
}
}
tmp.next = null; //防止最后一个节点也是重复的
return newHead.next;
}
Java:在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针
最新推荐文章于 2023-01-31 09:13:00 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)