add-two-numbersTwoSum

code

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
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
curFlag :=0
head := &ListNode{}
root := head
for l1!=nil || l2!=nil{
l1Val := 0
l2Val := 0
if l1 !=nil{
l1Val = l1.Val
l1 = l1.Next

}
if l2 != nil {
l2Val = l2.Val
l2 = l2.Next

}
temp := curFlag + l1Val + l2Val
if temp>=10{
temp -=10
curFlag = 1
}else{
curFlag = 0
}
tempNode := &ListNode{}
tempNode.Val = temp
root.Next = tempNode
root = root.Next
}
if curFlag ==1{
root.Next = &ListNode{
Val : 1,
}
}
return head.Next
}

add-two-numbersTwoSum
http://orikey0.github.io/2024/02/01/AddTwoNumbers/
作者
Howard Lee
发布于
2024年2月1日
许可协议