# Leetcode两数相加

1. 两数相加
给你两个 非空 的链表，表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的，并且每个节点只能存储 一位 数字。

0 <= Node.val <= 9

/**

• public class ListNode {

• int val;

• ListNode next;

• ListNode() {}

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

• ListNode(int val, ListNode next) { this.val = val; this.next = next; }

• }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {

 ListNode retListNode = new ListNode();
ListNode preNode = retListNode;
ListNode retNode = retListNode;
ListNode l1Iter = l1;
ListNode l2Iter = l2;
int val = -1;
while(l1Iter != null && l2Iter != null){
val = l1Iter.val + l2Iter.val + retListNode.val;
if(val >= 10){
val = val - 10;
}

retListNode.val = val;

ListNode node = new ListNode();
node.next = null;

retListNode.next = node;
preNode = retListNode;
retListNode = retListNode.next;

l1Iter = l1Iter.next;
l2Iter = l2Iter.next;
}

if(l1Iter != null){
while(l1Iter != null){
val = l1Iter.val + retListNode.val;
if(val >= 10){
val = val - 10;
}

retListNode.val = val;

ListNode node = new ListNode();
node.next = null;
retListNode.next = node;
preNode = retListNode;
retListNode = retListNode.next;
l1Iter = l1Iter.next;
}
}

if(l2Iter != null){
while(l2Iter != null){
val = l2Iter.val + retListNode.val;
if(val >= 10){
val = val - 10;
}

retListNode.val = val;

ListNode node = new ListNode();
node.next = null;
retListNode.next = node;
preNode = retListNode;
retListNode = retListNode.next;
l2Iter = l2Iter.next;
}
}

if(preNode.next != null && preNode.next.val == 0){
preNode.next = null;
}

return retNode;


}
}

12-22 35
05-06 4592
03-06 128
11-15 147
02-23 267
11-25 87
04-29 119
02-11 1万+
04-09 525
06-14 209
02-08 358