My java solution


  • 0
    J
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            if(l1 == null || l2 == null) return l1 == null? l2 : l1;
            ListNode dummy = new ListNode(0);
            ListNode node = dummy;
            int flag = 0;
            while(l1!=null || l2!=null || flag != 0){
                ListNode temp = new ListNode(0);
                int value = 0;
                if(l1 != null) {
                    value += l1.val;
                    l1 = l1.next;
                }
                if(l2 != null) {
                    value += l2.val;
                    l2 = l2.next;
                }
                value += flag;
                if(value > 9){
                    flag = 1;
                    value = value % 10;
                }else{
                    flag = 0;
                }
                temp.val = value;
                node.next = temp;
                node = node.next;
            }
            return dummy.next;
        }

Log in to reply
 

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