> For the complete documentation index, see [llms.txt](https://junnie.gitbook.io/amazon-mock-interview/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://junnie.gitbook.io/amazon-mock-interview/phone-interview-ii/167add-two-numbers.md).

# 167.Add Two Numbers

## 1.Description(Easy)

You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in`reverse`order, such that the 1's digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.

**Example**

Given`7->1->6 + 5->9->2`. That is,`617 + 295`.

Return`2->1->9`. That is`912`.

Given`3->1->5`and`5->9->2`, return`8->0->8`.

[**Tags**](https://www.lintcode.com/en/problem/add-two-numbers/#tags)

[Cracking The Coding Interview](https://www.lintcode.com/tag/cracking-the-coding-interview/) [Linked List](https://www.lintcode.com/tag/linked-list/) [High Precision](https://www.lintcode.com/tag/high-precision/)

## 2.Code

从l1, l2头节点开始，对应位置相加并建立新节点。用一个变量carry记录进位。注意几种特殊情况：

1.一个链表为空

l1: NULL

l2: 1->2

sum: 1->2

2.l1, l2长度不同，且结果有可能长度超过l1, l2中的最大长度

l1: 2->2

l2: 9->9->9

sum: 1->2->0->1

```
public ListNode addLists(ListNode l1, ListNode l2) {

        if(l1==null && l2==null){
            return null;
        }

        ListNode dummy=new ListNode(0);
        ListNode current=dummy;
        int carry=0; //控制进位
        while(l1!=null && l2!=null){
            int sum=l1.val+l2.val+carry;//注意每次都要加上carry
            carry=sum/10;
            current.next=new ListNode(sum%10);

            l1=l1.next;
            l2=l2.next;
            current=current.next;
        }

        while(l1!=null){
            int sum=l1.val+carry;
            carry=sum/10;
            current.next=new ListNode(sum%10);

            l1=l1.next;
            current=current.next;
        }

        while(l2!=null){
            int sum=l2.val+carry;
            carry=carry/10;
            current.next=new ListNode(sum%10);

            l2=l2.next;
            current=current.next;
        }

        //最后判断下是否还有进位
        if(carry!=0){
            current.next=new ListNode(carry);
        }
        return dummy.next;
    }
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://junnie.gitbook.io/amazon-mock-interview/phone-interview-ii/167add-two-numbers.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.
