clear java solution


  • 0
    _
    class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            int carryFlag = 0;
            ListNode result = l1;
            while(l2 != null || carryFlag > 0) {
                l1.val += (l2 == null ? 0 : l2.val) + carryFlag;
                carryFlag = l1.val >= 10 ? 1 : 0;
                l1.val = carryFlag == 1 ? l1.val -= 10 : l1.val;
                l2 = l2 == null ? null : l2.next;
                if(l1.next == null && (l2 != null || carryFlag > 0)) l1.next = new ListNode(0);
                l1 = l1.next;
            }
            return result;
        }
    }
    

Log in to reply
 

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