15.合并两个有序链表-递归
思路
package main
type ListNode struct {
Val int
Next *ListNode
}
// @solution-sync:begin
func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
var res *ListNode
if l1 == nil {
return l2
}
if l2 == nil {
return l1
}
if l1.Val < l2.Val {
res = l1
res.Next = mergeTwoLists(l1.Next, l2)
} else {
res = l2
res.Next = mergeTwoLists(l1, l2.Next)
}
return res
}
Last updated