-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.go
38 lines (30 loc) · 761 Bytes
/
2.go
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
// https://leetcode.com/problems/add-two-numbers/
package main
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
currentNode1 := l1
currentNode2 := l2
resultNodeHead := &ListNode{}
carry := 0
for resultNode := resultNodeHead; currentNode1 != nil || currentNode2 != nil || carry > 0; resultNode = resultNode.Next {
sum := 0
if currentNode1 != nil {
sum += currentNode1.Val
currentNode1 = currentNode1.Next
}
if currentNode2 != nil {
sum += currentNode2.Val
currentNode2 = currentNode2.Next
}
sum += carry
carry = sum / 10
resultNode.Next = &ListNode{Val: sum % 10}
}
return resultNodeHead.Next
}