File tree Expand file tree Collapse file tree 3 files changed +91
-0
lines changed
lcci/02.01.Remove Duplicate Node Expand file tree Collapse file tree 3 files changed +91
-0
lines changed Original file line number Diff line number Diff line change @@ -102,6 +102,38 @@ class Solution {
102102}
103103```
104104
105+ ### ** JavaScript**
106+
107+ ``` javascript
108+ /**
109+ * Definition for singly-linked list.
110+ * function ListNode(val) {
111+ * this.val = val;
112+ * this.next = null;
113+ * }
114+ */
115+ /**
116+ * @param {ListNode} head
117+ * @return {ListNode}
118+ */
119+ var removeDuplicateNodes = function (head ) {
120+ if (head == null || head .next == null ) return head;
121+ const cache = new Set ([]);
122+ cache .add (head .val );
123+ let cur = head, fast = head .next ;
124+ while (fast !== null ) {
125+ if (! cache .has (fast .val )) {
126+ cur .next = fast;
127+ cur = cur .next ;
128+ cache .add (fast .val );
129+ }
130+ fast = fast .next ;
131+ }
132+ cur .next = null ;
133+ return head;
134+ };
135+ ```
136+
105137### ** ...**
106138
107139```
Original file line number Diff line number Diff line change @@ -103,6 +103,38 @@ class Solution {
103103}
104104```
105105
106+ ### ** JavaScript**
107+
108+ ``` javascript
109+ /**
110+ * Definition for singly-linked list.
111+ * function ListNode(val) {
112+ * this.val = val;
113+ * this.next = null;
114+ * }
115+ */
116+ /**
117+ * @param {ListNode} head
118+ * @return {ListNode}
119+ */
120+ var removeDuplicateNodes = function (head ) {
121+ if (head == null || head .next == null ) return head;
122+ const cache = new Set ([]);
123+ cache .add (head .val );
124+ let cur = head, fast = head .next ;
125+ while (fast !== null ) {
126+ if (! cache .has (fast .val )) {
127+ cur .next = fast;
128+ cur = cur .next ;
129+ cache .add (fast .val );
130+ }
131+ fast = fast .next ;
132+ }
133+ cur .next = null ;
134+ return head;
135+ };
136+ ```
137+
106138### ** ...**
107139
108140```
Original file line number Diff line number Diff line change 1+ /**
2+ * Definition for singly-linked list.
3+ * function ListNode(val) {
4+ * this.val = val;
5+ * this.next = null;
6+ * }
7+ */
8+ /**
9+ * @param {ListNode } head
10+ * @return {ListNode }
11+ */
12+ var removeDuplicateNodes = function ( head ) {
13+ if ( head == null || head . next == null ) return head ;
14+ const cache = new Set ( [ ] ) ;
15+ cache . add ( head . val ) ;
16+ let cur = head , fast = head . next ;
17+ while ( fast !== null ) {
18+ if ( ! cache . has ( fast . val ) ) {
19+ cur . next = fast ;
20+ cur = cur . next ;
21+ cache . add ( fast . val ) ;
22+ }
23+ fast = fast . next ;
24+ }
25+ cur . next = null ;
26+ return head ;
27+ } ;
You can’t perform that action at this time.
0 commit comments