[JAVA] Super Easy


#1
/**
 * Definition for singly-linked list.
 * class ListNode {
 *     public int val;
 *     public ListNode next;
 *     ListNode(int x) { val = x; next = null; }
 * }
 */
public class Solution {
    
   
    public ListNode addTwoNumbers(ListNode A, ListNode B) {
        ListNode dummyhead = new ListNode(-1);
        ListNode head = dummyhead;
        int carry = 0;
        while(A!=null && B != null){
            int no1 = A.val;
            int no2 = B.val;
            int sum = no1+no2+carry;
            if(sum >= 10){
                carry = 1;
                // System.out.println(sum);
                head.next = new ListNode(sum%10);
            }
            else{
                carry = 0;
                head.next = new ListNode(sum);
            }
            A = A.next;
            B = B.next;
            head = head.next;
        }
        while(A!=null){
            int sum = A.val + carry;
            if(sum >= 10){
                carry = 1;
                head.next = new ListNode(sum%10);
            }
            else{
                carry = 0;
                head.next = new ListNode(sum);
            }
            A =A.next;
            head = head.next;
        }
        while(B!=null){
            int sum = B.val + carry;
            if(sum >= 10){
                carry = 1;
                head.next = new ListNode(sum%10);
            }
            else{
                carry = 0;
                head.next = new ListNode(sum);
            }
            B = B.next;
            head = head.next;
        }
        if(carry == 1){
            head.next = new ListNode(1);
            head = head.next;
        }
        
        return dummyhead.next;
    }
}