Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update solutions to lc problem: No.0021 #4038

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 128 additions & 33 deletions solution/0000-0099/0021.Merge Two Sorted Lists/README.md
Original file line number Diff line number Diff line change
@@ -295,24 +295,19 @@ var mergeTwoLists = function (list1, list2) {
*/
public class Solution {
public ListNode MergeTwoLists(ListNode list1, ListNode list2) {
ListNode dummy = new ListNode();
ListNode cur = dummy;
while (list1 != null && list2 != null)
{
if (list1.val <= list2.val)
{
cur.next = list1;
list1 = list1.next;
}
else
{
cur.next = list2;
list2 = list2.next;
}
cur = cur.next;
if (list1 == null) {
return list2;
}
if (list2 == null) {
return list1;
}
if (list1.val <= list2.val) {
list1.next = MergeTwoLists(list1.next, list2);
return list1;
} else {
list2.next = MergeTwoLists(list1, list2.next);
return list2;
}
cur.next = list1 == null ? list2 : list1;
return dummy.next;
}
}
```
@@ -332,23 +327,60 @@ public class Solution {
# @param {ListNode} list2
# @return {ListNode}
def merge_two_lists(list1, list2)
dummy = ListNode.new()
cur = dummy
while list1 && list2
if list1.val <= list2.val
cur.next = list1
list1 = list1.next
else
cur.next = list2
list2 = list2.next
end
cur = cur.next
if list1.nil?
return list2
end
if list2.nil?
return list1
end
if list1.val <= list2.val
list1.next = merge_two_lists(list1.next, list2)
return list1
else
list2.next = merge_two_lists(list1, list2.next)
return list2
end
cur.next = list1 || list2
dummy.next
end
```

#### PHP

```php
/**
* Definition for a singly-linked list.
* class ListNode {
* public $val = 0;
* public $next = null;
* function __construct($val = 0, $next = null) {
* $this->val = $val;
* $this->next = $next;
* }
* }
*/
class Solution {
/**
* @param ListNode $list1
* @param ListNode $list2
* @return ListNode
*/
function mergeTwoLists($list1, $list2) {
if (is_null($list1)) {
return $list2;
}
if (is_null($list2)) {
return $list1;
}
if ($list1->val <= $list2->val) {
$list1->next = $this->mergeTwoLists($list1->next, $list2);
return $list1;
} else {
$list2->next = $this->mergeTwoLists($list1, $list2->next);
return $list2;
}
}
}
```

<!-- tabs:end -->

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

#### C#

```cs
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode MergeTwoLists(ListNode list1, ListNode list2) {
ListNode dummy = new ListNode();
ListNode curr = dummy;
while (list1 != null && list2 != null) {
if (list1.val <= list2.val) {
curr.next = list1;
list1 = list1.next;
} else {
curr.next = list2;
list2 = list2.next;
}
curr = curr.next;
}
curr.next = list1 == null ? list2 : list1;
return dummy.next;
}
}
```

#### Ruby

```rb
# Definition for singly-linked list.
# class ListNode
# attr_accessor :val, :next
# def initialize(val = 0, _next = nil)
# @val = val
# @next = _next
# end
# end
# @param {ListNode} list1
# @param {ListNode} list2
# @return {ListNode}
def merge_two_lists(list1, list2)
dummy = ListNode.new()
cur = dummy
while list1 && list2
if list1.val <= list2.val
cur.next = list1
list1 = list1.next
else
cur.next = list2
list2 = list2.next
end
cur = cur.next
end
cur.next = list1 || list2
dummy.next
end
```

#### PHP

```php
@@ -616,18 +714,15 @@ var mergeTwoLists = function (list1, list2) {
# $this->next = $next;
# }
# }

class Solution {
/**
* @param ListNode $list1
* @param ListNode $list2
* @return ListNode
*/

function mergeTwoLists($list1, $list2) {
$dummy = new ListNode(0);
$current = $dummy;

while ($list1 != null && $list2 != null) {
if ($list1->val <= $list2->val) {
$current->next = $list1;
Loading