Skip to content

Commit ff12cf2

Browse files
committed
Add solution and test-cases and README for problem 21
1 parent 974dadb commit ff12cf2

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# [21.Merge Two Sorted Lists][title]
2+
3+
## Description
4+
You are given the heads of two sorted linked lists `list1` and `list2`.
5+
6+
Merge the two lists in a one **sorted** list. The list should be made by splicing together the nodes of the first two lists.
7+
8+
Return the head of the merged linked list.
9+
10+
**Example 1:**
11+
![exampl1](./merge_ex1.jpg)
12+
13+
```
14+
Input: list1 = [1,2,4], list2 = [1,3,4]
15+
Output: [1,1,2,3,4,4]
16+
```
17+
18+
**Example 2:**
19+
20+
```
21+
Input: list1 = [], list2 = []
22+
Output: []
23+
```
24+
25+
**Example 3:**
26+
```
27+
Input: list1 = [], list2 = [0]
28+
Output: [0]
29+
```
30+
31+
## 结语
32+
33+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
34+
35+
[title]: https://leetcode.com/problems/merge-two-sorted-lists/
36+
[me]: https://github.com/kylesliu/awesome-golang-algorithm

leetcode/1-100/0021.Merge-Two-Sorted-Lists/Solution.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,21 @@ func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
2727

2828
return head.Next
2929
}
30+
31+
func mergeTwoLists1(l1, l2 *ListNode) *ListNode {
32+
if l1 == nil {
33+
return l2
34+
}
35+
if l2 == nil {
36+
return l1
37+
}
38+
n := &ListNode{}
39+
if l1.Val < l2.Val {
40+
n.Val = l1.Val
41+
n.Next = mergeTwoLists1(l1.Next, l2)
42+
} else {
43+
n.Val = l2.Val
44+
n.Next = mergeTwoLists1(l1, l2.Next)
45+
}
46+
return n
47+
}

leetcode/1-100/0021.Merge-Two-Sorted-Lists/Solution_test.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ func TestSolution(t *testing.T) {
1010
data1 := []int{1, 2, 4}
1111
data2 := []int{1, 3, 4}
1212

13-
got := mergeTwoLists(MakeListNote(data1), MakeListNote(data2))
13+
l1, l2 := MakeListNote(data1), MakeListNote(data2)
14+
got := mergeTwoLists(l1, l2)
1415
want := MakeListNote([]int{1, 1, 2, 3, 4, 4})
1516

1617
if !Equal(got, want) {
@@ -20,13 +21,23 @@ func TestSolution(t *testing.T) {
2021
PrintList(want)
2122
t.Error("GOT:", got, "WANT:", want)
2223
}
24+
25+
got = mergeTwoLists1(l1, l2)
26+
if !Equal(got, want) {
27+
fmt.Print("GOT:")
28+
PrintList(got)
29+
fmt.Print("WANT:")
30+
PrintList(want)
31+
t.Error("GOT:", got, "WANT:", want)
32+
}
2333
})
2434

2535
t.Run("Test-2", func(t *testing.T) {
2636
data1 := []int{1, 2, 4, 5, 6, 7}
2737
data2 := []int{1, 3, 4, 9, 10}
2838

29-
got := mergeTwoLists(MakeListNote(data1), MakeListNote(data2))
39+
l1, l2 := MakeListNote(data1), MakeListNote(data2)
40+
got := mergeTwoLists(l1, l2)
3041
want := MakeListNote([]int{1, 1, 2, 3, 4, 4, 5, 6, 7, 9, 10})
3142

3243
if !Equal(got, want) {
@@ -36,6 +47,15 @@ func TestSolution(t *testing.T) {
3647
PrintList(want)
3748
t.Error("GOT:", got, "WANT:", want)
3849
}
50+
51+
got = mergeTwoLists1(l1, l2)
52+
if !Equal(got, want) {
53+
fmt.Print("GOT:")
54+
PrintList(got)
55+
fmt.Print("WANT:")
56+
PrintList(want)
57+
t.Error("GOT:", got, "WANT:", want)
58+
}
3959
})
4060
}
4161

25.8 KB
Loading

0 commit comments

Comments
 (0)