Skip to content

Commit 7b1d843

Browse files
committed
solve problem Merge Two Sorted Lists
1 parent 5f98de9 commit 7b1d843

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ All solutions will be accepted!
6868
|806|[Number Of Lines To Write String](https://leetcode-cn.com/problems/number-of-lines-to-write-string/description/)|[java/py/js](./algorithms/NumberOfLinesToWriteString)|Easy|
6969
|566|[Reshape The Matrix](https://leetcode-cn.com/problems/reshape-the-matrix/description/)|[java/py/js](./algorithms/ReshapeTheMatrix)|Easy|
7070
|575|[Distribute Candies](https://leetcode-cn.com/problems/distribute-candies/description/)|[java/py/js](./algorithms/DistributeCandies)|Easy|
71+
|21|[Merge Two Sorted Lists](https://leetcode-cn.com/problems/merge-two-sorted-lists/description/)|[java/py/js](./algorithms/MergeTwoSortedLists)|Easy|
7172

7273
# Database
7374
|#|Title|Solution|Difficulty|
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Merge Two Sorted Lists
2+
This problem is easy to solve by two linked list
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
11+
ListNode res = null,
12+
head = null;
13+
while (l1 != null && l2 != null) {
14+
if (res == null) {
15+
head = res = new ListNode(Math.min(l1.val, l2.val));
16+
} else {
17+
head.next = new ListNode(Math.min(l1.val, l2.val));
18+
head = head.next;
19+
}
20+
if (l1.val <= l2.val) {
21+
l1 = l1.next;
22+
} else {
23+
l2 = l2.next;
24+
}
25+
}
26+
27+
if (res == null) res = l1 != null ? l1 : l2;
28+
else head.next = l1 != null ? l1 : l2;
29+
return res;
30+
}
31+
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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} l1
10+
* @param {ListNode} l2
11+
* @return {ListNode}
12+
*/
13+
var mergeTwoLists = function(l1, l2) {
14+
let head = res = null
15+
16+
while (l1 !== null && l2 !== null) {
17+
if (res === null) {
18+
head = res = new ListNode(Math.min(l1.val, l2.val))
19+
} else {
20+
head.next = new ListNode(Math.min(l1.val, l2.val))
21+
head = head.next
22+
}
23+
if (l1.val <= l2.val) {
24+
l1 = l1.next
25+
} else {
26+
l2 = l2.next
27+
}
28+
}
29+
30+
if (res === null) {
31+
res = l1 !== null ? l1 : l2
32+
} else {
33+
head.next = l1 !== null ? l1 : l2
34+
}
35+
36+
return res
37+
};
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Definition for singly-linked list.
2+
# class ListNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution(object):
8+
def mergeTwoLists(self, l1, l2):
9+
"""
10+
:type l1: ListNode
11+
:type l2: ListNode
12+
:rtype: ListNode
13+
"""
14+
res = None
15+
head = None
16+
while l1 and l2:
17+
if res == None:
18+
res = ListNode(min(l1.val, l2.val))
19+
head = res
20+
else:
21+
head.next = ListNode(min(l1.val, l2.val))
22+
head = head.next
23+
if l1.val <= l2.val:
24+
l1 = l1.next
25+
else:
26+
l2 = l2.next
27+
if l1 and res:
28+
head.next = l1
29+
elif l2 and res:
30+
head.next = l2
31+
elif l1 and not res:
32+
res = l1
33+
elif l2 and not res:
34+
res = l2
35+
return res

0 commit comments

Comments
 (0)