My accepted golang solution


  • 0
    K

    Thoughts?

    /**
     * Definition for singly-linked list.
     * type ListNode struct {
     *     Val int
     *     Next *ListNode
     * }
     */
    
    func addUp(l1 *ListNode, l2 *ListNode, carryOver int) *ListNode {
        ln := ListNode{}
        
        sum := carryOver
        
        if (l1 != nil) {
            sum += l1.Val
            l1 = l1.Next
        }
        
        if (l2 != nil) {
            sum += l2.Val
            l2 = l2.Next
        }
        
        carryOver = sum / 10
        ln.Val = sum % 10
        
        if (l1 != nil || l2 != nil || carryOver > 0 ) {
            ln.Next = addUp(l1, l2, carryOver)
        }
        
        return &ln
    }
    
    func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
        answer := addUp(l1, l2, 0)
        return answer
    }
    

Log in to reply
 

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