Golang recursion solution


  • 1
    Y

    func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
    if l1 == nil && l2 == nil {
    return nil
    } else if l1 == nil {
    return l2
    } else if l2 == nil {
    return l1
    } else {
    n := &ListNode{l1.Val + l2.Val, addTwoNumbers(l1.Next, l2.Next)}
    if n.Val > 9 {
    n = &ListNode{n.Val - 10, addTwoNumbers(n.Next, &ListNode{1, nil})}
    }
    return n
    }
    }


Log in to reply
 

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