-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathaddTwoNumbers.go
More file actions
52 lines (43 loc) · 1.22 KB
/
Copy pathaddTwoNumbers.go
File metadata and controls
52 lines (43 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* https://leetcode.com/problems/add-two-numbers/#/description
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
给你两个表示两个非负数字的链表。数字以相反的顺序存储,其节点包含单个数字。将这两个数字相加并将其作为一个链表返回。
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
*/
package lll
import "fmt"
type ListNode struct {
Val int
Next *ListNode
}
func (l *ListNode) String() string {
return fmt.Sprintf("%d %s", l.Val, l.Next)
}
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
var (
result, current *ListNode
sum = 0
carry = 0
)
for l1 != nil || l2 != nil || carry != 0 {
sum = carry
if l1 != nil {
sum += l1.Val
l1 = l1.Next
}
if l2 != nil {
sum += l2.Val
l2 = l2.Next
}
carry = sum / 10
temp := &ListNode{Val: sum % 10}
if result == nil {
result = temp
} else {
current.Next = temp
}
current = temp
}
return result
}