给定一个排序链表,删除所有重复元素,使得每个元素只出现一次。
重复的情况下指向下一个指针
- 时间复杂度 O(1),空间复杂度O(1)
- Runtime: 92 ms, faster than 64.01%
- Memory Usage: 40.4 MB, less than 70.73%
/**
* 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) {
let res = head
while(head && head.next) {
while(head.next && head.val === head.next.val) {
head.next = head.next.next
}
head = head.next
}
return res
};