力扣试题–删除排序链表中的重复元素(js版本)
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
function ListNode(val, next) {
this.val = (val === undefined ? 0 : val)
this.next = (next === undefined ? null : next)
}
/**
* @param {ListNode} head
* @return {ListNode}
*/
var deleteDuplicates = function (head) {
if (!head) {
return head;
}
let p = head;
while (p.next) {
if (p.val != p.next.val) {
p = p.next;
} else {
p.next = p.next.next; // 注意这里:指针不要移晕了
}
}
return head;
};