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 7b0f65d

Browse files
committedFeb 3, 2023
Add solution #82
1 parent 7928a3f commit 7b0f65d

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
78|[Subsets](./0078-subsets.js)|Medium|
7878
80|[Remove Duplicates from Sorted Array II](./0080-remove-duplicates-from-sorted-array-ii.js)|Medium|
7979
81|[Search in Rotated Sorted Array II](./0081-search-in-rotated-sorted-array-ii.js)|Medium|
80+
82|[Remove Duplicates from Sorted List II](./0082-remove-duplicates-from-sorted-list-ii.js)|Medium|
8081
83|[Remove Duplicates from Sorted List](./0083-remove-duplicates-from-sorted-list.js)|Easy|
8182
86|[Partition List](./0086-partition-list.js)|Medium|
8283
88|[Merge Sorted Array](./0088-merge-sorted-array.js)|Easy|
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* 82. Remove Duplicates from Sorted List II
3+
* https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/
4+
* Difficulty: Medium
5+
*
6+
* Given the head of a sorted linked list, delete all nodes that have duplicate
7+
* numbers, leaving only distinct numbers from the original list. Return the
8+
* linked list sorted as well.
9+
*/
10+
11+
/**
12+
* Definition for singly-linked list.
13+
* function ListNode(val, next) {
14+
* this.val = (val===undefined ? 0 : val)
15+
* this.next = (next===undefined ? null : next)
16+
* }
17+
*/
18+
/**
19+
* @param {ListNode} head
20+
* @return {ListNode}
21+
*/
22+
var deleteDuplicates = function(head) {
23+
const result = new ListNode();
24+
let previous = null;
25+
let tail = result;
26+
27+
while (head) {
28+
if (head.val !== head.next?.val && head.val !== previous) {
29+
tail.next = new ListNode(head.val);
30+
tail = tail.next;
31+
}
32+
previous = head.val;
33+
head = head.next;
34+
}
35+
36+
return result.next;
37+
};

0 commit comments

Comments
 (0)
Please sign in to comment.