83.清理排序链表中的数据元素(javascript)83.RemoveDuplicatesfromSortedList
原创给定排序列表的标题 head , 删除所有重复的元素,以便每个元素只显示一次。 。返回 排序的链接列表 。
Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.
示例 1:
输入:head = [1,1,2]
输出:[1,2]
示例 2:
输入:head = [1,1,2,3,3]
输出:[1,2,3]
/**
* 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
}
var cur = head
while (cur.next) {
if (cur.val == cur.next.val) {
cur.next = cur.next.next
} else {
cur = cur.next
}
}
return head
};
leetcode: https://leetcode.cn/problems/remove-duplicates-from-sorted-list/
版权声明
所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除