# 两数相加

### 题目：

• 链表遍历，最后结果返回头
• 两个链表不一定等长
• 考虑进位

Java实现

``````/**
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) { val = x; }
* }
*/
public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {

ListNode next1 = l1, next2 = l2, sumNext = resultHeader;
int up, sum;
boolean doSum = true;
while (doSum) {
if (next1 == null && next2 != null) {
sum = sumNext.val + next2.val;
next2 = next2.next;
} else if (next1 != null && next2 == null) {
sum = sumNext.val + next1.val;
next1 = next1.next;
} else {
sum = next1.val + next2.val + sumNext.val;
next1 = next1.next;
next2 = next2.next;
}
up = sum / 10;
sumNext.val = up > 0 ? sum - 10 : sum;
if ((doSum = next1 != null || next2 != null) || up > 0) {
sumNext.next = new ListNode(up);
sumNext = sumNext.next;
}
}
}
``````

Golang实现

``````/**
* type ListNode struct {
*     Val int
*     Next *ListNode
* }
*/
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
next1, next2, sumNext := l1, l2, &header
up, sum := 0, 0
doSum := true
for doSum {
if next1 == nil && next2 != nil {
sum = sumNext.Val + next2.Val
next2 = next2.Next
} else if next2 == nil && next1 != nil {
sum = sumNext.Val + next1.Val
next1 = next1.Next
} else {
sum = sumNext.Val + next1.Val + next2.Val
next2 = next2.Next
next1 = next1.Next
}
up = sum / 10
if up > 0 {
sumNext.Val = sum - 10
} else {
sumNext.Val = sum
}
doSum = next1 != nil || next2 != nil
if doSum || up > 0 {
sumNext.Next = &ListNode{up, nil}
sumNext = sumNext.Next
}
}
}
``````

0 回复

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、``单行代码``
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传

### 题目：

• 链表遍历，最后结果返回头
• 两个链表不一定等长
• 考虑进位

Java实现

``````/**
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) { val = x; }
* }
*/
public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {

ListNode next1 = l1, next2 = l2, sumNext = resultHeader;
int up, sum;
boolean doSum = true;
while (doSum) {
if (next1 == null && next2 != null) {
sum = sumNext.val + next2.val;
next2 = next2.next;
} else if (next1 != null && next2 == null) {
sum = sumNext.val + next1.val;
next1 = next1.next;
} else {
sum = next1.val + next2.val + sumNext.val;
next1 = next1.next;
next2 = next2.next;
}
up = sum / 10;
sumNext.val = up > 0 ? sum - 10 : sum;
if ((doSum = next1 != null || next2 != null) || up > 0) {
sumNext.next = new ListNode(up);
sumNext = sumNext.next;
}
}
}
``````

Golang实现

``````/**
* type ListNode struct {
*     Val int
*     Next *ListNode
* }
*/
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
next1, next2, sumNext := l1, l2, &header
up, sum := 0, 0
doSum := true
for doSum {
if next1 == nil && next2 != nil {
sum = sumNext.Val + next2.Val
next2 = next2.Next
} else if next2 == nil && next1 != nil {
sum = sumNext.Val + next1.Val
next1 = next1.Next
} else {
sum = sumNext.Val + next1.Val + next2.Val
next2 = next2.Next
next1 = next1.Next
}
up = sum / 10
if up > 0 {
sumNext.Val = sum - 10
} else {
sumNext.Val = sum
}
doSum = next1 != nil || next2 != nil
if doSum || up > 0 {
sumNext.Next = &ListNode{up, nil}
sumNext = sumNext.Next
}
}