parent
e357d81606
commit
7b82c24907
@ -0,0 +1,42 @@
|
||||
package mergesorted
|
||||
|
||||
type ListNode struct {
|
||||
Val int
|
||||
Next *ListNode
|
||||
}
|
||||
|
||||
func reverse(list *ListNode) *ListNode {
|
||||
var result *ListNode
|
||||
for list != nil {
|
||||
result = &ListNode{Val: list.Val, Next: result}
|
||||
list = list.Next
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode {
|
||||
|
||||
var result *ListNode
|
||||
for list1 != nil || list2 != nil {
|
||||
if list1 == nil {
|
||||
result = &ListNode{Val: list2.Val, Next: result}
|
||||
list2 = list2.Next
|
||||
} else if list2 == nil {
|
||||
result = &ListNode{Val: list1.Val, Next: result}
|
||||
list1 = list1.Next
|
||||
} else if list1.Val < list2.Val {
|
||||
result = &ListNode{Val: list1.Val, Next: result}
|
||||
list1 = list1.Next
|
||||
} else {
|
||||
result = &ListNode{Val: list2.Val, Next: result}
|
||||
list2 = list2.Next
|
||||
}
|
||||
}
|
||||
|
||||
return reverse(result)
|
||||
}
|
||||
|
||||
func MergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode {
|
||||
return mergeTwoLists(list1, list2)
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package mergesorted_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
ms "git.jxs.me/leetgo/mergesorted"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func buildList(values []int) *ms.ListNode {
|
||||
var result *ms.ListNode
|
||||
|
||||
for i := len(values) - 1; i >= 0; i-- {
|
||||
result = &ms.ListNode{Val: values[i], Next: result}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func TestEmpty(t *testing.T) {
|
||||
require.Nil(t, nil, ms.MergeTwoLists(nil, nil))
|
||||
}
|
||||
|
||||
func TestEmptyRight(t *testing.T) {
|
||||
left := buildList([]int{4, 5})
|
||||
right := buildList([]int{})
|
||||
res := ms.MergeTwoLists(left, right)
|
||||
require.Equal(t, left, res)
|
||||
}
|
||||
|
||||
func TestEmptyLeft(t *testing.T) {
|
||||
left := buildList([]int{})
|
||||
right := buildList([]int{1, 2})
|
||||
res := ms.MergeTwoLists(left, right)
|
||||
require.Equal(t, right, res)
|
||||
}
|
||||
|
||||
func TestMix(t *testing.T) {
|
||||
left := buildList([]int{1, 3})
|
||||
right := buildList([]int{2, 4})
|
||||
res := ms.MergeTwoLists(left, right)
|
||||
|
||||
expected := buildList([]int{1, 2, 3, 4})
|
||||
require.Equal(t, expected, res)
|
||||
}
|
||||
|
||||
func TestExample1(t *testing.T) {
|
||||
left := buildList([]int{1, 3, 4})
|
||||
right := buildList([]int{1, 2, 4})
|
||||
res := ms.MergeTwoLists(left, right)
|
||||
|
||||
expected := buildList([]int{1, 1, 2, 3, 4, 4})
|
||||
require.Equal(t, expected, res)
|
||||
}
|
||||
|
||||
func TestExample2(t *testing.T) {
|
||||
left := buildList([]int{})
|
||||
right := buildList([]int{0})
|
||||
res := ms.MergeTwoLists(left, right)
|
||||
|
||||
expected := buildList([]int{0})
|
||||
require.Equal(t, expected, res)
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
21. Merge Two Sorted Lists
|
||||
Easy
|
||||
|
||||
You are given the heads of two sorted linked lists list1 and list2.
|
||||
|
||||
Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists.
|
||||
|
||||
Return the head of the merged linked list.
|
||||
|
||||
|
||||
|
||||
Example 1:
|
||||
|
||||
Input: list1 = [1,2,4], list2 = [1,3,4]
|
||||
Output: [1,1,2,3,4,4]
|
||||
|
||||
Example 2:
|
||||
|
||||
Input: list1 = [], list2 = []
|
||||
Output: []
|
||||
|
||||
Example 3:
|
||||
|
||||
Input: list1 = [], list2 = [0]
|
||||
Output: [0]
|
||||
|
||||
|
||||
|
||||
Constraints:
|
||||
|
||||
The number of nodes in both lists is in the range [0, 50].
|
||||
-100 <= Node.val <= 100
|
||||
Both list1 and list2 are sorted in non-decreasing order.
|
Loading…
Reference in new issue