Skip to content

Commit 179f91b

Browse files
authored
feat: update solutions to lc problem: No.0021 (#4038)
1 parent 2bfa7a9 commit 179f91b

File tree

8 files changed

+399
-128
lines changed

8 files changed

+399
-128
lines changed

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

+128-33
Original file line numberDiff line numberDiff line change
@@ -295,24 +295,19 @@ var mergeTwoLists = function (list1, list2) {
295295
*/
296296
public class Solution {
297297
public ListNode MergeTwoLists(ListNode list1, ListNode list2) {
298-
ListNode dummy = new ListNode();
299-
ListNode cur = dummy;
300-
while (list1 != null && list2 != null)
301-
{
302-
if (list1.val <= list2.val)
303-
{
304-
cur.next = list1;
305-
list1 = list1.next;
306-
}
307-
else
308-
{
309-
cur.next = list2;
310-
list2 = list2.next;
311-
}
312-
cur = cur.next;
298+
if (list1 == null) {
299+
return list2;
300+
}
301+
if (list2 == null) {
302+
return list1;
303+
}
304+
if (list1.val <= list2.val) {
305+
list1.next = MergeTwoLists(list1.next, list2);
306+
return list1;
307+
} else {
308+
list2.next = MergeTwoLists(list1, list2.next);
309+
return list2;
313310
}
314-
cur.next = list1 == null ? list2 : list1;
315-
return dummy.next;
316311
}
317312
}
318313
```
@@ -332,23 +327,60 @@ public class Solution {
332327
# @param {ListNode} list2
333328
# @return {ListNode}
334329
def merge_two_lists(list1, list2)
335-
dummy = ListNode.new()
336-
cur = dummy
337-
while list1 && list2
338-
if list1.val <= list2.val
339-
cur.next = list1
340-
list1 = list1.next
341-
else
342-
cur.next = list2
343-
list2 = list2.next
344-
end
345-
cur = cur.next
330+
if list1.nil?
331+
return list2
332+
end
333+
if list2.nil?
334+
return list1
335+
end
336+
if list1.val <= list2.val
337+
list1.next = merge_two_lists(list1.next, list2)
338+
return list1
339+
else
340+
list2.next = merge_two_lists(list1, list2.next)
341+
return list2
346342
end
347-
cur.next = list1 || list2
348-
dummy.next
349343
end
350344
```
351345

346+
#### PHP
347+
348+
```php
349+
/**
350+
* Definition for a singly-linked list.
351+
* class ListNode {
352+
* public $val = 0;
353+
* public $next = null;
354+
* function __construct($val = 0, $next = null) {
355+
* $this->val = $val;
356+
* $this->next = $next;
357+
* }
358+
* }
359+
*/
360+
class Solution {
361+
/**
362+
* @param ListNode $list1
363+
* @param ListNode $list2
364+
* @return ListNode
365+
*/
366+
function mergeTwoLists($list1, $list2) {
367+
if (is_null($list1)) {
368+
return $list2;
369+
}
370+
if (is_null($list2)) {
371+
return $list1;
372+
}
373+
if ($list1->val <= $list2->val) {
374+
$list1->next = $this->mergeTwoLists($list1->next, $list2);
375+
return $list1;
376+
} else {
377+
$list2->next = $this->mergeTwoLists($list1, $list2->next);
378+
return $list2;
379+
}
380+
}
381+
}
382+
```
383+
352384
<!-- tabs:end -->
353385

354386
<!-- solution:end -->
@@ -603,6 +635,72 @@ var mergeTwoLists = function (list1, list2) {
603635
};
604636
```
605637

638+
#### C#
639+
640+
```cs
641+
/**
642+
* Definition for singly-linked list.
643+
* public class ListNode {
644+
* public int val;
645+
* public ListNode next;
646+
* public ListNode(int val=0, ListNode next=null) {
647+
* this.val = val;
648+
* this.next = next;
649+
* }
650+
* }
651+
*/
652+
public class Solution {
653+
public ListNode MergeTwoLists(ListNode list1, ListNode list2) {
654+
ListNode dummy = new ListNode();
655+
ListNode curr = dummy;
656+
while (list1 != null && list2 != null) {
657+
if (list1.val <= list2.val) {
658+
curr.next = list1;
659+
list1 = list1.next;
660+
} else {
661+
curr.next = list2;
662+
list2 = list2.next;
663+
}
664+
curr = curr.next;
665+
}
666+
curr.next = list1 == null ? list2 : list1;
667+
return dummy.next;
668+
}
669+
}
670+
```
671+
672+
#### Ruby
673+
674+
```rb
675+
# Definition for singly-linked list.
676+
# class ListNode
677+
# attr_accessor :val, :next
678+
# def initialize(val = 0, _next = nil)
679+
# @val = val
680+
# @next = _next
681+
# end
682+
# end
683+
# @param {ListNode} list1
684+
# @param {ListNode} list2
685+
# @return {ListNode}
686+
def merge_two_lists(list1, list2)
687+
dummy = ListNode.new()
688+
cur = dummy
689+
while list1 && list2
690+
if list1.val <= list2.val
691+
cur.next = list1
692+
list1 = list1.next
693+
else
694+
cur.next = list2
695+
list2 = list2.next
696+
end
697+
cur = cur.next
698+
end
699+
cur.next = list1 || list2
700+
dummy.next
701+
end
702+
```
703+
606704
#### PHP
607705

608706
```php
@@ -616,18 +714,15 @@ var mergeTwoLists = function (list1, list2) {
616714
# $this->next = $next;
617715
# }
618716
# }
619-
620717
class Solution {
621718
/**
622719
* @param ListNode $list1
623720
* @param ListNode $list2
624721
* @return ListNode
625722
*/
626-
627723
function mergeTwoLists($list1, $list2) {
628724
$dummy = new ListNode(0);
629725
$current = $dummy;
630-
631726
while ($list1 != null && $list2 != null) {
632727
if ($list1->val <= $list2->val) {
633728
$current->next = $list1;

0 commit comments

Comments
 (0)