My golang solution 25ms


  • 0
    func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
        l3 := &ListNode{}
        tail3 := l3
        num := 0
        for ;l1 != nil || l2 != nil;{
            num /= 10
            if l1 != nil {
                num += l1.Val
                l1 = l1.Next
            } 
            if l2 != nil {
                num += l2.Val
                l2 = l2.Next
            }
            node := &ListNode{}
            node.Val = num % 10
            tail3.Next = node
            tail3 = node
        }
        if num/10 == 1 {
            node := &ListNode{}
            node.Val = 1
            tail3.Next = node
            tail3 = node
        }
        return l3.Next
    }
    

Log in to reply
 

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