Golang solution (6ms)


  • 0
    func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
        var dummyHead ListNode
        prev := &dummyHead
        for l1 != nil && l2 != nil {
            if l1.Val < l2.Val {
                prev.Next = l1
                l1 = l1.Next
            } else {
                prev.Next = l2
                l2 = l2.Next
            }
            prev = prev.Next
        }
        if l1 != nil {
            prev.Next = l1
        } else {
            prev.Next = l2
        }
        return dummyHead.Next
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.