-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path83_javascript.js
50 lines (44 loc) · 1023 Bytes
/
83_javascript.js
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。
// 示例 1:
// 输入: 1->1->2
// 输出: 1->2
// 示例 2:
// 输入: 1->1->2->3->3
// 输出: 1->2->3
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
// 链表简单知识,题目中写明是排序链表
var deleteDuplicates = function (head) {
if (head == null) return head;
let current = head;
while (current.next != null) {
if (current.val == current.next.val) {
current.next = current.next.next;
} else {
current = current.next;
}
}
return head;
};
var deleteDuplicates = function (head) {
let list = new ListNode(null);
list.next = head;
while (list) {
if (list.next && list.val == list.next.val) {
// 主要在于此处判断
list.next = list.next.next;
} else {
list = list.next;
}
}
return head;
};