-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path83-remove-duplicates-sorted-list.js
More file actions
58 lines (50 loc) · 1.08 KB
/
83-remove-duplicates-sorted-list.js
File metadata and controls
58 lines (50 loc) · 1.08 KB
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
/**
* 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}
*/
var deleteDuplicates = function (head) {
let res = head;
while (res) {
// tenemos que checar si nex existe antes de leer su valor.
if (res.next && res.val === res.next.val) {
res.next = res.next.next;
} else {
res = res.next;
}
}
return head;
};
function print_list(list) {
// console.log("print_list1");
let r = "";
let head = list;
while (head) {
r += head.val;
if (head.next) {
r += " -> ";
}
head = head.next;
}
return r;
}
function ListNode(val, next) {
this.val = val === undefined ? 0 : val;
this.next = next === undefined ? null : next;
}
const n5 = new ListNode(3);
const n4 = new ListNode(3);
n4.next = n5;
const n3 = new ListNode(2);
n3.next = n4;
const n2 = new ListNode(1);
n2.next = n3;
const n1 = new ListNode(1);
n1.next = n2;
console.log(print_list(deleteDuplicates(n1)));