1. Title Description

You are given two non-empty linked lists representing two non-negative integers. Each digit is stored in reverse order, and only one digit can be stored per node.

You add the two numbers and return a linked list representing the sum in the same form.

You can assume that neither of these numbers will start with 0, except for the number 0.

Example 1:

Input: l1 = [2,4,3], l2 = [5,6,4] output: [7,0,8] explanation: 342 + 465 = 807. Example 2:

Input: L1 = [0], L2 = [0] Output: [0] Example 3:

Input: l1 =,9,9,9,9,9,9 [9], l2 =,9,9,9 [9] output:,9,9,9,0,0,0,1 [8]

Tip:

The number of nodes in each linked list is in the range [1, 100] 0 <= node. val <= 9

Source: LeetCode link: leetcode-cn.com/problems/ad… Copyright belongs to the Collar buckle network. Commercial reprint please contact official authorization, non-commercial reprint please indicate the source.

Second, complete code

public class Solution {

    public static void main(String[] args) {

        int ln_val1[] = {2.4.3};
        int ln_val2[] = {5.6.4};
        ListNodeAdd listNodeAdd = new ListNodeAdd();
        ListNode ln1 = listNodeAdd.generateListNode(ln_val1);
        listNodeAdd.printLN(ln1);
        ListNode ln2 = listNodeAdd.generateListNode(ln_val2);
        listNodeAdd.printLN(ln2);

        System.out.println("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ="); ListNode ln_sum = listNodeAdd.addTwoNumbers(ln1, ln2); listNodeAdd.printLN(ln_sum); }}class ListNodeAdd {


    public ListNode generateListNode(int val[]) {
        ListNode head = null;
        ListNode tail = null;
        for (int i = 0; i < val.length; i++) {
            if (head == null) {
                ListNode lb_data = new ListNode(val[i]);
                tail = lb_data;
                head = tail;
            } else {
                ListNode lb_data = newListNode(val[i]); tail.next = lb_data; tail = tail.next; }}return head;
    }

    public void printLN(ListNode ln) {
        if (ln == null) {
            System.out.println("ListNode is null");
            return;
        }
        String str_val = "";
        while(ln ! =null) {
            str_val = str_val + ln.val + "";
            ln = ln.next;
        }
        System.out.println("Data is: " + str_val);
    }


    public ListNode addTwoNumbers(ListNode ln1, ListNode ln2) {
        ListNode head = null;
        ListNode cur = null;
        int carry_number = 0;
        while(ln1 ! =null|| ln2 ! =null) {
            int num1 = ln1 == null ? 0 : ln1.val;
            int num2 = ln2 == null ? 0 : ln2.val;
            int sum = num1 + num2 + carry_number;
            carry_number = 0;
            ListNode ln_data = null;
            if (head == null) {
                if (sum >= 10) {
                    ln_data = new ListNode(sum - 10);
                    cur = ln_data;
                    head = cur;
                    carry_number = 1;
                } else {
                    ln_data = newListNode(sum); cur = ln_data; head = cur; }}else {
                if (sum >= 10) {
                    ln_data = new ListNode(sum - 10);
                    cur.next = ln_data;
                    cur = cur.next;
                    carry_number = 1;
                } else {
                    ln_data = newListNode(sum); cur.next = ln_data; cur = cur.next; }}if(ln1 ! =null) {
                ln1 = ln1.next;
            }
            if(ln2 ! =null) { ln2 = ln2.next; }}if (carry_number > 0) {
            cur.next = new ListNode(carry_number);
        }
        returnhead; }}class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next; }}Copy the code