Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 58b6b5d

Browse files
committedSep 28, 2021
Add solution #83
1 parent ab56f0a commit 58b6b5d

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
66|[Plus One](./0066-plus-one.js)|Easy|
3636
67|[Add Binary](./0067-add-binary.js)|Easy|
3737
73|[Set Matrix Zeroes](./0073-set-matrix-zeroes.js)|Medium|
38+
83|[Remove Duplicates from Sorted List](./0083-remove-duplicates-from-sorted-list.js)|Easy|
3839
88|[Merge Sorted Array](./0088-merge-sorted-array.js)|Easy|
3940
118|[Pascal's Triangle](./0118-pascals-triangle.js)|Easy|
4041
119|[Pascal's Triangle II](./0119-pascals-triangle-ii.js)|Easy|
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* 83. Remove Duplicates from Sorted List
3+
* https://leetcode.com/problems/remove-duplicates-from-sorted-list/
4+
* Difficulty: Easy
5+
*
6+
* Given the head of a sorted linked list, delete all duplicates such that each
7+
* element appears only once. Return the linked list sorted as well.
8+
*/
9+
10+
/**
11+
* Definition for singly-linked list.
12+
* function ListNode(val, next) {
13+
* this.val = (val===undefined ? 0 : val)
14+
* this.next = (next===undefined ? null : next)
15+
* }
16+
*/
17+
/**
18+
* @param {ListNode} head
19+
* @return {ListNode}
20+
*/
21+
var deleteDuplicates = function(head) {
22+
const result = new ListNode();
23+
const set = new Set();
24+
let tail = result;
25+
26+
while (head) {
27+
if (!set.has(head.val)) {
28+
tail.next = new ListNode(head.val);
29+
tail = tail.next;
30+
}
31+
32+
set.add(head.val);
33+
34+
head = head.next;
35+
}
36+
37+
return result.next;
38+
};

0 commit comments

Comments
 (0)
Please sign in to comment.