「这是我参与11月更文挑战的第8天,活动详情查看:2021最后一次更文挑战」
题目:83. 删除排序链表中的重复元素
解答
function deleteDuplicates(head) {
if (!head) return head;
let cur = head;
while (cur.next) {
if (cur.val === cur.next.val) {
cur.next = cur.next.next;
} else {
cur = cur.next;
}
}
return head;
}
题目:82. 删除排序链表中的重复元素 II
解答:
function deleteDuplicates(head) {
if (!head) {
return head;
}
const dummy = new ListNode(0, head);
let cur = dummy;
while (cur.next && cur.next.next) {
if (cur.next.val === cur.next.next.val) {
const x = cur.next.val;
while (cur.next && cur.next.val === x) {
cur.next = cur.next.next;
}
} else {
cur = cur.next;
}
}
return dummy.next;
};