Problem List

Merge Two Sorted Lists

May 31, 2025Go linked list, recursioneasy

Problem

Approach

Performance

Complexity

Go Solution
func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode {
    if list1 == nil {
        return list2
    }
    
    if list2 == nil {
        return list1
    }

    var node *ListNode
    if list1.Val < list2.Val {
        node = list1
        node.Next = mergeTwoLists(list1.Next, list2)
    } else {
        node = list2
        node.Next = mergeTwoLists(list1, list2.Next)
    }

    return node
}
LeetCode Problem Link