File tree 2 files changed +38
-0
lines changed
2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change 77
77
78|[ Subsets] ( ./0078-subsets.js ) |Medium|
78
78
80|[ Remove Duplicates from Sorted Array II] ( ./0080-remove-duplicates-from-sorted-array-ii.js ) |Medium|
79
79
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|
80
81
83|[ Remove Duplicates from Sorted List] ( ./0083-remove-duplicates-from-sorted-list.js ) |Easy|
81
82
86|[ Partition List] ( ./0086-partition-list.js ) |Medium|
82
83
88|[ Merge Sorted Array] ( ./0088-merge-sorted-array.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments