题目要求
思路
I'm a loser,have no idea
不会做,,,所以选择了“逃课”,用数组
代码参考
// 没想到好的解决方法,用数组“逃课”,先把所有的存数组,然后遍历数组,有重复的就跳过,我这里做了一个标记,给他设置为1,以后再访问到他直接跳过
function deleteDuplicates( head ) {
if(!head || !head.next) return head
const result = []
let h = head
while(h) {
let temp = h.next
h.next = null
result.push(h.val)
h = temp
}
const res = []
const use = {}
result.forEach(item=>{
use[item] = 0
})
for(let i = 0; i < result.length; i++) {
if(use[result[i]]) continue
if(result.slice(i+1).includes(result[i])) {
use[result[i]] = 1
}
else res.push(result[i])
}
let node = new ListNode(-1)
let p = node
res.forEach(item=>{
p.next = new ListNode(item)
p = p.next
})
return node.next
}
标签:II,BM16,use,head,next,链表,item,let,result
From: https://www.cnblogs.com/zx529/p/17013089.html