public ListNode deleteDuplicates(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode newHead = new ListNode(0);
newHead.next = head;
ListNode cur = head;
ListNode last = newHead;
while (cur != null && cur.next != null) {
//如果相邻节点不相等,则当前节点赋值给新的的节点值
if (cur.val != cur.next.val) {
last = cur;
} else {
//如果相等,则next赋值删除后节点
while (cur.next != null && cur.val == cur.next.val) {
cur = cur.next;
}
//再赋值给新的节点
last.next = cur.next;
}
cur = cur.next;
}
return newHead.next;
}
删除有序链表中重复的元素-II
最新推荐文章于 2024-08-07 10:50:39 发布