Skip to content

Commit 1c9126f

Browse files
committed
feat: update solutions to lc problem: No.0023. Merge k Sorted Lists
1 parent 9d4d8ea commit 1c9126f

File tree

12 files changed

+684
-203
lines changed

12 files changed

+684
-203
lines changed

solution/0000-0099/0021.Merge Two Sorted Lists/README.md

+34
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,40 @@ def merge_two_lists(l1, l2)
242242
end
243243
```
244244

245+
### **C#**
246+
247+
```cs
248+
/**
249+
* Definition for singly-linked list.
250+
* public class ListNode {
251+
* public int val;
252+
* public ListNode next;
253+
* public ListNode(int val=0, ListNode next=null) {
254+
* this.val = val;
255+
* this.next = next;
256+
* }
257+
* }
258+
*/
259+
public class Solution {
260+
public ListNode MergeTwoLists(ListNode l1, ListNode l2) {
261+
ListNode dummy = new ListNode();
262+
ListNode cur = dummy;
263+
while (l1 != null && l2 != null) {
264+
if (l1.val <= l2.val) {
265+
cur.next = l1;
266+
l1 = l1.next;
267+
} else {
268+
cur.next = l2;
269+
l2 = l2.next;
270+
}
271+
cur = cur.next;
272+
}
273+
cur.next = l1 == null ? l2 : l1;
274+
return dummy.next;
275+
}
276+
}
277+
```
278+
245279
### **...**
246280

247281
```

solution/0000-0099/0021.Merge Two Sorted Lists/README_EN.md

+34
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,40 @@ def merge_two_lists(l1, l2)
230230
end
231231
```
232232

233+
### **C#**
234+
235+
```cs
236+
/**
237+
* Definition for singly-linked list.
238+
* public class ListNode {
239+
* public int val;
240+
* public ListNode next;
241+
* public ListNode(int val=0, ListNode next=null) {
242+
* this.val = val;
243+
* this.next = next;
244+
* }
245+
* }
246+
*/
247+
public class Solution {
248+
public ListNode MergeTwoLists(ListNode l1, ListNode l2) {
249+
ListNode dummy = new ListNode();
250+
ListNode cur = dummy;
251+
while (l1 != null && l2 != null) {
252+
if (l1.val <= l2.val) {
253+
cur.next = l1;
254+
l1 = l1.next;
255+
} else {
256+
cur.next = l2;
257+
l2 = l2.next;
258+
}
259+
cur = cur.next;
260+
}
261+
cur.next = l1 == null ? l2 : l1;
262+
return dummy.next;
263+
}
264+
}
265+
```
266+
233267
### **...**
234268

235269
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* public int val;
5+
* public ListNode next;
6+
* public ListNode(int val=0, ListNode next=null) {
7+
* this.val = val;
8+
* this.next = next;
9+
* }
10+
* }
11+
*/
12+
public class Solution {
13+
public ListNode MergeTwoLists(ListNode l1, ListNode l2) {
14+
ListNode dummy = new ListNode();
15+
ListNode cur = dummy;
16+
while (l1 != null && l2 != null) {
17+
if (l1.val <= l2.val) {
18+
cur.next = l1;
19+
l1 = l1.next;
20+
} else {
21+
cur.next = l2;
22+
l2 = l2.next;
23+
}
24+
cur = cur.next;
25+
}
26+
cur.next = l1 == null ? l2 : l1;
27+
return dummy.next;
28+
}
29+
}

solution/0000-0099/0023.Merge k Sorted Lists/README.md

+219-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ class Solution:
7979
lists[i] = self.mergeTwoLists(lists[i - 1], lists[i])
8080
return lists[n - 1]
8181

82-
8382
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
8483
dummy = ListNode()
8584
cur = dummy
@@ -123,7 +122,225 @@ class Solution {
123122
}
124123

125124
private ListNode mergeTwoLists(ListNode l1, ListNode l2) {
126-
ListNode dummy = new ListNode(0);
125+
ListNode dummy = new ListNode();
126+
ListNode cur = dummy;
127+
while (l1 != null && l2 != null) {
128+
if (l1.val <= l2.val) {
129+
cur.next = l1;
130+
l1 = l1.next;
131+
} else {
132+
cur.next = l2;
133+
l2 = l2.next;
134+
}
135+
cur = cur.next;
136+
}
137+
cur.next = l1 == null ? l2 : l1;
138+
return dummy.next;
139+
}
140+
}
141+
```
142+
143+
### **C++**
144+
145+
```cpp
146+
/**
147+
* Definition for singly-linked list.
148+
* struct ListNode {
149+
* int val;
150+
* ListNode *next;
151+
* ListNode() : val(0), next(nullptr) {}
152+
* ListNode(int x) : val(x), next(nullptr) {}
153+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
154+
* };
155+
*/
156+
class Solution {
157+
public:
158+
ListNode* mergeKLists(vector<ListNode*>& lists) {
159+
int n = lists.size();
160+
if (n == 0) {
161+
return nullptr;
162+
}
163+
for (int i = 1; i < n; ++i) {
164+
lists[i] = mergeTwoLists(lists[i - 1], lists[i]);
165+
}
166+
return lists[n - 1];
167+
}
168+
169+
private:
170+
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
171+
ListNode* dummy = new ListNode();
172+
ListNode* cur = dummy;
173+
while (l1 && l2) {
174+
if (l1->val <= l2->val) {
175+
cur->next = l1;
176+
l1 = l1->next;
177+
} else {
178+
cur->next = l2;
179+
l2 = l2->next;
180+
}
181+
cur = cur->next;
182+
}
183+
cur->next = l1 ? l1 : l2;
184+
return dummy->next;
185+
}
186+
};
187+
```
188+
189+
### **Go**
190+
191+
```go
192+
/**
193+
* Definition for singly-linked list.
194+
* type ListNode struct {
195+
* Val int
196+
* Next *ListNode
197+
* }
198+
*/
199+
func mergeKLists(lists []*ListNode) *ListNode {
200+
n := len(lists)
201+
if n == 0 {
202+
return nil
203+
}
204+
for i := 1; i < n; i++ {
205+
lists[i] = mergeTwoLists(lists[i-1], lists[i])
206+
}
207+
return lists[n-1]
208+
}
209+
210+
func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
211+
dummy := &ListNode{}
212+
cur := dummy
213+
for l1 != nil && l2 != nil {
214+
if l1.Val <= l2.Val {
215+
cur.Next = l1
216+
l1 = l1.Next
217+
} else {
218+
cur.Next = l2
219+
l2 = l2.Next
220+
}
221+
cur = cur.Next
222+
}
223+
if l1 != nil {
224+
cur.Next = l1
225+
} else if l2 != nil {
226+
cur.Next = l2
227+
}
228+
return dummy.Next
229+
}
230+
```
231+
232+
### **JavaScript**
233+
234+
```js
235+
/**
236+
* Definition for singly-linked list.
237+
* function ListNode(val, next) {
238+
* this.val = (val===undefined ? 0 : val)
239+
* this.next = (next===undefined ? null : next)
240+
* }
241+
*/
242+
/**
243+
* @param {ListNode[]} lists
244+
* @return {ListNode}
245+
*/
246+
var mergeKLists = function(lists) {
247+
const n = lists.length;
248+
if (n == 0) {
249+
return null;
250+
}
251+
for (let i = 1; i < n; ++i) {
252+
lists[i] = mergeTwoLists(lists[i - 1], lists[i]);
253+
}
254+
return lists[n - 1];
255+
};
256+
257+
function mergeTwoLists(l1, l2) {
258+
const dummy = new ListNode();
259+
let cur = dummy;
260+
while (l1 && l2) {
261+
if (l1.val <= l2.val) {
262+
cur.next = l1;
263+
l1 = l1.next;
264+
} else {
265+
cur.next = l2;
266+
l2 = l2.next;
267+
}
268+
cur = cur.next;
269+
}
270+
cur.next = l1 || l2;
271+
return dummy.next;
272+
}
273+
```
274+
275+
### **Ruby**
276+
277+
```rb
278+
# Definition for singly-linked list.
279+
# class ListNode
280+
# attr_accessor :val, :next
281+
# def initialize(val = 0, _next = nil)
282+
# @val = val
283+
# @next = _next
284+
# end
285+
# end
286+
# @param {ListNode[]} lists
287+
# @return {ListNode}
288+
def merge_k_lists(lists)
289+
n = lists.length
290+
i = 1
291+
while i < n
292+
lists[i] = merge_two_lists(lists[i - 1], lists[i])
293+
i += 1
294+
end
295+
lists[n - 1]
296+
end
297+
298+
def merge_two_lists(l1, l2)
299+
dummy = ListNode.new()
300+
cur = dummy
301+
while l1 && l2
302+
if l1.val <= l2.val
303+
cur.next = l1
304+
l1 = l1.next
305+
else
306+
cur.next = l2
307+
l2 = l2.next
308+
end
309+
cur = cur.next
310+
end
311+
cur.next = l1 || l2
312+
dummy.next
313+
end
314+
```
315+
316+
### **C#**
317+
318+
```cs
319+
/**
320+
* Definition for singly-linked list.
321+
* public class ListNode {
322+
* public int val;
323+
* public ListNode next;
324+
* public ListNode(int val=0, ListNode next=null) {
325+
* this.val = val;
326+
* this.next = next;
327+
* }
328+
* }
329+
*/
330+
public class Solution {
331+
public ListNode MergeKLists(ListNode[] lists) {
332+
int n = lists.Length;
333+
if (n == 0) {
334+
return null;
335+
}
336+
for (int i = 1; i < n; ++i) {
337+
lists[i] = MergeTwoLists(lists[i - 1], lists[i]);
338+
}
339+
return lists[n - 1];
340+
}
341+
342+
private ListNode MergeTwoLists(ListNode l1, ListNode l2) {
343+
ListNode dummy = new ListNode();
127344
ListNode cur = dummy;
128345
while (l1 != null && l2 != null) {
129346
if (l1.val <= l2.val) {

0 commit comments

Comments
 (0)