forked from LockGit/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_two_number.go
110 lines (98 loc) · 2.5 KB
/
add_two_number.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* Created by GoLand.
* User: lock
* Date: 2018/8/21
* Time: 00:03
* 将获得两个非空链接列表,表示两个非负整数。 数字以相反的顺序存储,并且它们的每个节点包含单个数字。 添加两个数字并将其作为链接列表返回。
* https://www.jianshu.com/p/1a4c349ae859
* Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
* Output: 7 -> 0 -> 8
*/
package main
import (
"fmt"
)
type ElementType interface{}
type Node struct {
Data ElementType
next *Node
}
type LinkedList struct {
head *Node
}
func CreateLinkedList() *LinkedList {
head := new(Node)
return &LinkedList{head}
}
func getLinkedListOne() *LinkedList {
node1 := CreateLinkedList()
node2 := CreateLinkedList()
node3 := CreateLinkedList()
node4 := CreateLinkedList()
node4.head.Data = nil
node4.head.next = nil
node1.head.Data = 2
node1.head.next = node2.head
node2.head.Data = 4
node2.head.next = node3.head
node3.head.Data = 3
node3.head.next = node4.head
return node1
}
func getLinkedListTwo() *LinkedList {
node1 := CreateLinkedList()
node2 := CreateLinkedList()
node3 := CreateLinkedList()
node4 := CreateLinkedList()
node4.head.Data = nil
node4.head.next = nil
node1.head.Data = 5
node1.head.next = node2.head
node2.head.Data = 6
node2.head.next = node3.head
node3.head.Data = 4
node3.head.next = node4.head
return node1
}
func CreateNewLinkedList(num int) *LinkedList {
node := CreateLinkedList()
node.head.Data = num
return node
}
func main() {
linkedListOne := getLinkedListOne().head
linkedListTwo := getLinkedListTwo().head
carry := 0
var res LinkedList
var resEnd LinkedList
for linkedListOne.next != nil || linkedListTwo.next != nil || carry == 1 {
tmp := 0
if linkedListOne.next != nil && linkedListOne.Data != nil {
tmp = tmp + linkedListOne.Data.(int)
linkedListOne = linkedListOne.next
}
if linkedListTwo.next != nil && linkedListTwo.Data != nil {
tmp = tmp + linkedListTwo.Data.(int)
linkedListTwo = linkedListTwo.next
}
tmp = tmp + carry
digit := tmp % 10
carry = tmp / 10
if res.head == nil {
res.head = CreateNewLinkedList(digit).head
resEnd.head = res.head
} else {
resEnd.head.next = CreateNewLinkedList(digit).head
resEnd.head = resEnd.head.next
}
}
//创建一个空链表链接到尾部,后面遍历链表for循环有!=nil判断
endNode := CreateLinkedList().head
resEnd.head.next = endNode
fmt.Println("打印链表数据:")
resNode := res.head
for resNode.next != nil {
fmt.Println(resNode.Data)
resNode = resNode.next
}
}