c# recursive implementation


  • 0
    R
     public static ListNode AddTwoNumbers(ListNode l1, ListNode l2)
            {
                ListNode currenListNodeSum = new ListNode(l1.val + l2.val);
    
                if (l1.next != null || l2.next != null || currenListNodeSum.val >= 10)
                {
                    l1.next = l1.next ?? new ListNode(0);
                    l2.next = l2.next ?? new ListNode(0);
    
                    if (currenListNodeSum.val >= 10)
                    {
                        l1.next.val += 1;
                        currenListNodeSum.val -= 10;
                    }
                       
                    currenListNodeSum.next =  AddTwoNumbers(l1.next, l2.next);
                }
    
                return currenListNodeSum;
            }
    

Log in to reply
 

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