-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_two_numbers.go
45 lines (38 loc) · 1.09 KB
/
add_two_numbers.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
39
40
41
42
43
44
45
package problem0002
import . "leetcodedaily/helpers/listnode"
/*
https://leetcode.com/problems/add-two-numbers/
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 contains a single digit.
Add the two numbers and return the sum as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
*/
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
var carry int
newList := &ListNode{}
cur := newList
for l1 != nil || l2 != nil {
var aVal, bVal, rem int
// getting digits from current node
// if node does not exist, digit is 0
if l1 != nil {
aVal = l1.Val
l1 = l1.Next
}
if l2 != nil {
bVal = l2.Val
l2 = l2.Next
}
// summing up the digits
rem = (aVal + bVal + carry) % 10
// carry goes into next node
carry = (aVal + bVal + carry) / 10
cur.Next = &ListNode{Val: rem}
cur = cur.Next
}
// accounting for last digits summing up to more than 10
if carry > 0 {
cur.Next = &ListNode{Val: carry}
}
return newList.Next
}