-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path删除列表中的重复节点.js
More file actions
30 lines (29 loc) · 864 Bytes
/
删除列表中的重复节点.js
File metadata and controls
30 lines (29 loc) · 864 Bytes
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
29
30
/*
题目描述
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。
例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
*/
function deleteDuplication(pHead)
{
if(!pHead || !pHead.next) return pHead;
let pre = {};
pre.next = pHead;
let init = pre;
let current = pHead, last = current.next;
while(last){
if(current.val === last.val){
while(last && current.val == last.val){
last = last.next;
current.next = last;
}
last = last ? last.next : null;
current = current.next;
pre.next = current;
} else {
pre = pre.next;
current = current.next;
last = last.next;
}
}
return init.next;
}