@@ -118,7 +118,7 @@ class Solution {
118
118
slow. next = null ;
119
119
ListNode l1 = sortList(head);
120
120
ListNode l2 = sortList(t);
121
- ListNode dummy = new ListNode (0 );
121
+ ListNode dummy = new ListNode ();
122
122
ListNode cur = dummy;
123
123
while (l1 != null && l2 != null ) {
124
124
if (l1. val <= l2. val) {
@@ -136,6 +136,103 @@ class Solution {
136
136
}
137
137
```
138
138
139
+ ### ** JavaScript**
140
+
141
+ ``` js
142
+ /**
143
+ * Definition for singly-linked list.
144
+ * function ListNode(val, next) {
145
+ * this.val = (val===undefined ? 0 : val)
146
+ * this.next = (next===undefined ? null : next)
147
+ * }
148
+ */
149
+ /**
150
+ * @param {ListNode} head
151
+ * @return {ListNode}
152
+ */
153
+ var sortList = function (head ) {
154
+ if (! head || ! head .next ) {
155
+ return head;
156
+ }
157
+ let slow = head;
158
+ let fast = head .next ;
159
+ while (fast && fast .next ) {
160
+ slow = slow .next ;
161
+ fast = fast .next .next ;
162
+ }
163
+ let t = slow .next ;
164
+ slow .next = null ;
165
+ let l1 = sortList (head);
166
+ let l2 = sortList (t);
167
+ const dummy = new ListNode ();
168
+ let cur = dummy;
169
+ while (l1 && l2) {
170
+ if (l1 .val <= l2 .val ) {
171
+ cur .next = l1;
172
+ l1 = l1 .next ;
173
+ } else {
174
+ cur .next = l2;
175
+ l2 = l2 .next ;
176
+ }
177
+ cur = cur .next ;
178
+ }
179
+ cur .next = l1 || l2;
180
+ return dummy .next ;
181
+ };
182
+ ```
183
+
184
+ ### ** C#**
185
+
186
+ ``` cs
187
+ /**
188
+ * Definition for singly-linked list.
189
+ * public class ListNode {
190
+ * public int val;
191
+ * public ListNode next;
192
+ * public ListNode(int val=0, ListNode next=null) {
193
+ * this.val = val;
194
+ * this.next = next;
195
+ * }
196
+ * }
197
+ */
198
+ public class Solution {
199
+ public ListNode SortList (ListNode head ) {
200
+ if (head == null || head .next == null )
201
+ {
202
+ return head ;
203
+ }
204
+ ListNode slow = head , fast = head .next ;
205
+ while (fast != null && fast .next != null )
206
+ {
207
+ slow = slow .next ;
208
+ fast = fast .next .next ;
209
+ }
210
+ ListNode t = slow .next ;
211
+ slow .next = null ;
212
+ ListNode l1 = SortList (head );
213
+ ListNode l2 = SortList (t );
214
+ ListNode dummy = new ListNode ();
215
+ ListNode cur = dummy ;
216
+ while (l1 != null && l2 != null )
217
+ {
218
+ if (l1 .val <= l2 .val )
219
+ {
220
+ cur .next = l1 ;
221
+ l1 = l1 .next ;
222
+ }
223
+ else
224
+ {
225
+ cur .next = l2 ;
226
+ l2 = l2 .next ;
227
+ }
228
+ cur = cur .next ;
229
+ }
230
+ cur .next = l1 == null ? l2 : l1 ;
231
+ return dummy .next ;
232
+ }
233
+ }
234
+ ```
235
+
139
236
### ** ...**
140
237
141
238
```
0 commit comments