forked from JoshCrozier/leetcode-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0083-remove-duplicates-from-sorted-list.js
49 lines (43 loc) · 1.1 KB
/
0083-remove-duplicates-from-sorted-list.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
/**
* 83. Remove Duplicates from Sorted List
* https://leetcode.com/problems/remove-duplicates-from-sorted-list/
* Difficulty: Easy
*
* Given the head of a sorted linked list, delete all duplicates such that each
* element appears only once. Return the linked list sorted as well.
*/
/**
* 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) {
const result = new ListNode();
let tail = result;
while (head) {
if (head.val !== head.next?.val) {
tail.next = new ListNode(head.val);
tail = tail.next;
}
previous = head.val;
head = head.next;
}
return result.next;
};
// var deleteDuplicates = function(head) {
// let current = head
// while (current && current.next) {
// if (current.val === current.next.val) {
// current.next = current.next.next
// } else {
// current = current.next
// }
// }
// return head
// };