# 9. 两数相加

java

```java
//------Link工具类------
public class LinkedUtils {

    /**
     * 数组转化为链表
     * @param array
     * @return
     */
    public static ListNode arrayToLinkedList(int[] array){
        if(array==null) return null;
        //head node 
        ListNode root=new ListNode(-1);
        ListNode p=root;

        int size=array.length;
        int i=0;
        while(i<size){
            ListNode q=new ListNode(array[i++]);
            q.next=null;
            p.next=q;
            p=q;
        }

        return root.next;
    }

    /**
     * 打印链表
     * @param root
     */
    public static void print(ListNode root) {
        if(root==null) System.out.println("root is null");
        else{
            while(root!=null){
                System.out.print(root.val);
                if(root.next!=null){
                    System.out.print("->");
                }
                root=root.next;
            }
            System.out.println();
        }
    }

    /**
     * 求链表长度
     * @param root
     * @return
     */
    public static int length(ListNode root) {
        if(root==null) return 0;
        int n=0;
        while(root!=null){
            root=root.next;
            n++;
        }
        return n;
    }

    /**
     * 返回指向链表尾部的指针
     * @param root
     * @return
     */
    public static ListNode moveToTail(ListNode root) {
        if(root==null) return null;
        ListNode tail=root;
        while(tail.next!=null){
            tail=tail.next;
        }
        return tail;
    }
}


//------节点定义类------
public class ListNode {
    public int val;
    public ListNode next;

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



//------真正开始问题------

/**
 * 两数相加.
 * https://leetcode-cn.com/problems/add-two-numbers/description/
 *
 * 给定两个非空链表来表示两个非负整数。位数按照逆序方式存储，它们的每个节点只存储单个数字。将两数相加返回一个新的链表。
 * 你可以假设除了数字 0 之外，这两个数字都不会以零开头。
 *
 * 示例：
 * 输入：(2 -> 4 -> 3) + (5 -> 6 -> 4)
 * 输出：7 -> 0 -> 8
 * 原因：342 + 465 = 807
 */
public class Solution2 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode node = new ListNode(-1);//头节点
        ListNode p = node;//指示执行到的位置
        int plusOne = 0;//是否进位

        while (l1 != null || l2 != null) {
            //计算对应位和
            int v1 = (l1 != null) ? l1.val : 0;
            int v2 = (l2 != null) ? l2.val : 0;
            int r = v1 + v2 + plusOne;

            //之和大于9，减去十即为该值，并进位
            if (r > 9) {
                plusOne = 1;
                ListNode tmp = new ListNode(r - 10);
                p.next = tmp;
                p = tmp;
            } else {
                plusOne = 0;
                ListNode tmp = new ListNode(r);
                p.next = tmp;
                p = tmp;
            }
            if (l1 != null) l1 = l1.next;
            if (l2 != null) l2 = l2.next;
        }

        //如果最后一位仍然需要进位，创建节点
        if (plusOne == 1) {
            p.next = new ListNode(1);
        }
        return node.next;
    }

    public static void main(String[] args) {
        //你的算法必须是高效的，否则下面这个测试集是过不去的
        int[] list1 = {
                2,4,3,2,4,3,2,4,3,2,
                4,3,2,4,3,2,4,3,2,4,
                3,2,4,3,2,4,3,2,4,3,
                2,4,3,2,4,3,2,4,3,2,
                4,3,2,4,3,2,4,3,2,4,
                3,2,4,3,2,4,3,2,4,3,9 };
        int[] list2 = {
                5,6,4,2,4,3,2,4,3,2,
                4,3,2,4,3,2,4,3,2,4,
                3,2,4,3,2,4,3,2,4,3,
                2,4,3,2,4,3,2,4,3,2,
                4,3,2,4,3,2,4,3,2,4,
                3,2,4,3,2,4,3,9,9,9,9 };

        ListNode l1 = LinkedUtils.arrayToLinkedList(list1);
        ListNode l2 = LinkedUtils.arrayToLinkedList(list2);

        ListNode root = new Solution2().addTwoNumbers(l1, l2);
        LinkedUtils.print(root);
    }
}
```

python

```python
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/shua-ti/zhi-shang-lei/9-liang-shu-xiang-jia.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
