My easy JAVA solution


  • 0
    T

    My easy Java solution.

    public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int carry = 0;
        ListNode temp = new ListNode(0);
        ListNode head = temp;
        while(l1 != null || l2 != null || carry != 0){
            int value = (l1 == null ? 0: l1.val) + (l2 == null ? 0: l2.val) + carry;
            carry = 0;
            carry = value/10;
            value = value%10;
            temp.next = new ListNode(value);
            temp = temp.next;
            l1 = l1 != null?l1.next:l1;
            l2 = l2 != null?l2.next:l2;
        }
        head = head.next;
        return head;
    }
    }

Log in to reply
 

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