---
# System prepended metadata

title: Add Two Numbers
tags: [linkedlist, medium, LeetCode]

---

#  Add Two Numbers
###### tags: `LeetCode` `medium` `linkedlist`
>Golang
```go=
/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */

func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
    head:=&ListNode{Val:0,Next:nil}
    cur:=head
    carry:=0
    for l1!=nil||l2!=nil {
        var x,y int
        if l1!=nil{
            x=l1.Val
        }else{
            x=0
        }
        if l2!=nil{
            y=l2.Val
        }else{
            y=0
        }
        sum:=x+y+carry
        carry=sum/10
        cur.Next=&ListNode{Val:sum%10,Next:nil}
        cur=cur.Next
        if l1!=nil{
            l1=l1.Next
        }
        if l2!=nil{
            l2=l2.Next
        }
    }
    if carry==1{
        cur.Next=&ListNode{Val:1,Next:nil}
    }
    return head.Next
}
```
:::info
Runtime: 12 ms, faster than 60.49% of Go online submissions for Add Two Numbers.
Memory Usage: 5 MB, less than 12.20% of Go online submissions for Add Two Numbers.
:::

:::danger
15~24行 必須判斷 l1 l2 是否為空，不然讀取到 nil 會造成 segmentation fault

29~32行 同上必須判斷是否為空，不然也會造成 segmentation fault

36~38行 則是如果 carry 還是1時，則要多創一個 node  Val=1  
適用情況為 [5] [7] ans: [2][1]，如果少了就會變成 ans:[2]
算是例外狀況
:::

>cpp

```cpp=
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode *head= new ListNode(0);
        ListNode *cur=head;
        int carry=0;
        while(l1!=NULL||l2!=NULL){
            int x=(l1!=NULL)?l1->val:0;
            int y=(l2!=NULL)?l2->val:0;
            int sum=x+y+carry;
            carry=sum/10;
            cur->next=new ListNode(sum%10);
            cur=cur->next;
            l1=l1?l1->next:l1;
            l2=l2?l2->next:l2;
        }
        if(carry>0)
            cur->next=new ListNode(carry);
         return head->next;
    }
};


```
:::info
Runtime: 24 ms, faster than 90.22% of C++ online submissions for Add Two Numbers.
Memory Usage: 11.1 MB, less than 5.14% of C++ online submissions for Add Two Numbers.
:::
:::danger
注意事項相同，只是變更為三元條件
:::

>c language
>
```c=

  /*struct ListNode {
      int val;
      struct ListNode *next;
  };*/
 


struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
    int carry = 0;
    int outNum = 0;
    int num1 = 0;
    int num2 = 0;
    struct ListNode* resultList = NULL;
    struct ListNode* ptr = NULL;
    struct ListNode* priPtr = NULL;
    
    resultList = (struct ListNode*)malloc(sizeof(struct ListNode));
    ptr = resultList;
    while(l1 != NULL || l2 != NULL){
        num1 = 0;
        num2 = 0;
        if(l1 != NULL){
           num1 = l1->val;
           l1 = l1->next;
        }
        if(l2 != NULL){
           num2 = l2->val;
           l2 = l2->next;
        }
        int tmp = num1 + num2 + carry;
        if(tmp >= 10){
            outNum = tmp - 10;
            carry = 1;
        }else{
            outNum = tmp;
            carry = 0;
        }
        ptr->val = outNum;
        struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));
        if(!node)
            return NULL;
        node->next = NULL;
        ptr->next = node;
        priPtr = ptr;
        ptr = node;
    }
    if(carry == 1){
       ptr->val = carry; 
    }else{
        priPtr->next = NULL;
        free(ptr);
    }
    
    
    return resultList;
}

```

:::info
Runtime: 16 ms, faster than 52.16% of C online submissions for Add Two Numbers.
Memory Usage: 7.5 MB, less than 100.00% of C online submissions for Add Two Numbers.
:::
