题解 | #删除有序链表中重复的元素-II#
删除有序链表中重复的元素-II
https://www.nowcoder.com/practice/71cef9f8b5564579bf7ed93fbe0b2024
/*
* function ListNode(x){
* this.val = x;
* this.next = null;
* }
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @return ListNode类
*/
function deleteDuplicates( head ) {
// write code here
if (!head || !head.next) {
return head;
}
const dummy = new ListNode(0);
dummy.next = head;
let prev = dummy;
while (head !== null) {
let hasDup = false;
while (head.next !== null && head.val === head.next.val) {
hasDup = true;
head = head.next;
}
if (hasDup) {
prev.next = head.next;
} else {
prev = prev.next;
}
head = head.next;
}
return dummy.next;
}
module.exports = {
deleteDuplicates : deleteDuplicates
};
检查是否有重复元素,跳过重复元素
SHEIN希音公司福利 370人发布