-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSolution.js
63 lines (56 loc) · 1.56 KB
/
Solution.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
51
52
53
54
55
56
57
58
59
60
61
62
63
// https://leetcode.com/problems/remove-duplicates-from-sorted-list/
/**
* 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}
*/
function ListNode(val, next) {
this.val = (val === undefined ? 0 : val)
this.next = (next === undefined ? null : next)
}
// https://leetcode.com/problems/remove-duplicates-from-sorted-list/discuss/2442595/JavaScript-Fast-and-simple-O(n)-time-O(1)-space-solution
var deleteDuplicates2 = function (head) {
if (!head) {
return null;
}
let cur = head;
while (cur?.next) {
let nextNode = cur.next;
while (nextNode && nextNode.val === cur.val) {
nextNode = nextNode.next;
}
cur.next = nextNode;
cur = nextNode;
}
return head;
};
// https://leetcode.com/problems/remove-duplicates-from-sorted-list/discuss/28722/Javascript-Solution
var deleteDuplicates = function (head) {
var cur = head;
while (cur) {
if (cur.next !== null && cur.val == cur.next.val) {
cur.next = cur.next.next;
} else {
cur = cur.next;
}
}
return head;
};
// [1,1,2,3,3] // [1,2,3]
let node1 = new ListNode(1);
let node2 = new ListNode(1);
let node3 = new ListNode(2);
let node4 = new ListNode(3);
let node5 = new ListNode(3);
node1.next = node2;
node2.next = node3
node3.next = node4;
node4.next = node5;
// console.log(node1);
console.log(deleteDuplicates(node1));