import java.util.*;
/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Plus {
    public ListNode plusAB(ListNode a, ListNode b) {
        // write code here
        ListNode ch = null;
        ListNode ce = null;
        int jw = 0;
        while (a != null && b != null) {
            int sum = (jw + a.val + b.val) % 10;
            jw = (jw + a.val + b.val) / 10;
            if (ch == null) {
                ListNode node = new ListNode(sum);
                ch = node;
                ce = ch;
            } else {
                ListNode node = new ListNode(sum);
                ce.next = node;
                ce = node;
            }
            a = a.next;
            b = b.next;
        }
        while (a != null) {
            int sum = (jw + a.val) % 10;
            jw = 0;
            if (ch == null) {
                ListNode node = new ListNode(sum);
                ch = node;
                ce = ch;
            } else {
                ListNode node = new ListNode(sum);
                ce.next = node;
                ce = node;
            }
            a = a.next;
        }

        while (b != null) {
            int sum = (jw + b.val) % 10;
            jw = 0;
            if (ch == null) {
                ListNode node = new ListNode(sum);
                ch = node;
                ce = ch;
            } else {
                ListNode node = new ListNode(sum);
                ce.next = node;
                ce = node;
            }
            b = b.next;
        }

        if (jw != 0) {
            ListNode node = new ListNode(jw);
            ce.next = node;
            ce = node;
        }
        return ch;
    }
}