//Java
public ListNode deleteDuplicates(ListNode head) {
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode prev = dummy;
ListNode curr = head;
while (curr != null && curr.next != null) {
if (curr.val == curr.next.val) {
ListNode temp = curr.next;
while (temp != null && temp.val == curr.val) {
temp = temp.next;
}
prev.next = temp;
curr = temp;
} else {
prev = prev.next;
curr = curr.next;
}
}
return dummy.next;
}
算法:删除有序链表中重复出现的元素
最新推荐文章于 2023-12-06 09:29:38 发布