012_合并两个有序链表

知识点:链表、递归

LeetCode第二十一题:https://leetcode-cn.com/problems/merge-two-sorted-lists/submissions/

语言:GoLang

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
    if l1 == nil {
        return l2
    }
    if l2 == nil {
        return l1
    }

    if l1.Val > l2.Val {
        l2.Next = mergeTwoLists(l1, l2.Next)
        return l2
    }else {
        l1.Next = mergeTwoLists(l1.Next, l2)
        return l1
    }
}
posted @ 2020-03-08 20:11  Cenyol  阅读(79)  评论(0)    收藏  举报