File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * function ListNode(val, next) {
4
+ * this.val = (val===undefined ? 0 : val)
5
+ * this.next = (next===undefined ? null : next)
6
+ * }
7
+ */
8
+ /**
9
+ * @param {ListNode } head
10
+ * @return {ListNode }
11
+ */
12
+ const deleteDuplicatesUnsorted = function ( head ) {
13
+ const set = new Set ( )
14
+ const del = new Set ( )
15
+ let cur = head
16
+
17
+ while ( cur ) {
18
+ if ( set . has ( cur . val ) ) {
19
+ del . add ( cur . val )
20
+ } else {
21
+ set . add ( cur . val )
22
+
23
+ }
24
+ cur = cur . next
25
+ }
26
+
27
+ const dummy = new ListNode ( )
28
+ dummy . next = head
29
+ cur = dummy
30
+
31
+ while ( cur ) {
32
+ if ( cur . next ) {
33
+ if ( del . has ( cur . next . val ) ) {
34
+ cur . next = cur . next . next
35
+ } else {
36
+ cur = cur . next
37
+ }
38
+ } else {
39
+ cur = cur . next
40
+ }
41
+ }
42
+
43
+ return dummy . next
44
+ } ;
You can’t perform that action at this time.
0 commit comments