Leetcode题解之 —— 删除排序链表中的重复元素
思路
两种解法
- 暴力遍历
- 数组接管
题解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
var deleteDuplicates = function (head) { let cache = []; let current = head;
while (current) { cache.push(current.val ? current.val : 0); current = current.next; }
if (!cache.length) { return null; }
cache = [...new Set(cache)];
const final = cache .map((v) => new ListNode(v)) .map((v, i, a) => { v.next = a[i + 1] ? a[i + 1] : null; return v; })
return final[0]; };
|