题目
给定一个已排序的链表的头 head , 删除所有重复的元素,使每个元素只出现一次 。返回 已排序的链表 。
答案
/**
* Definition for singly-linked list.
* 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 cur = head;
while(cur.next){
if(cur.val == cur.next.val){
cur.next = cur.next.next
}else{
cur = cur.next
}
}
return head;
}在这里插入代码片