-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode2
More file actions
151 lines (138 loc) · 4.17 KB
/
leetcode2
File metadata and controls
151 lines (138 loc) · 4.17 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# Definition for singly-linked list.
# Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
# Output: 7 -> 0 -> 8
# Explanation: 342 + 465 = 807.
class ListNode(object): #定义点
def __init__(self, x):
self.val = x
self.next = None
class NList(): #定义链表存储结构
def __init__(self,node):
self.head=node
def add_node(self,addedNode):
tempnode=self.head
while tempnode.next!=None:
tempnode=tempnode.next
tempnode.next=addedNode
return self
def print_order_list(self):
tempnode=self.head
print(tempnode.val)
while tempnode.next!=None:
tempnode = tempnode.next
print(tempnode.val)
def get_len(self):
tempnode=self.head
length=1
while tempnode.next!=None:
tempnode=tempnode.next
length+=1
return length
Head1=ListNode(2)
H1=NList(Head1)
H1.add_node(ListNode(4)).add_node(ListNode(3))
# H1.print_order_list()
#342+65=407 这里使用 65因为 它更具有一般性不要全部位数相同的相加
H2=NList(ListNode(5))
H2.add_node(ListNode(6)).add_node(ListNode(4)).add_node(ListNode(3))
# H2.print_order_list()
# print(H2.get_len())
def sum_Nlist(H1,H2):
result_head=NList(ListNode(-1))
res_H=result_head.head
len1=H1.get_len()
len2=H2.get_len()
tempnode1=H1.head
tempnode2=H2.head
signal_add=False
while tempnode1!=None and tempnode2!=None:#边界条件为任何一个没有子节点了
add_res=tempnode1.val+tempnode2.val
if signal_add==True:
add_res=add_res+1
if add_res>=10:
tempvalue=add_res%10
signal_add=True
else:
tempvalue=add_res
signal_add=False
if res_H.val==-1: #结果的头节点未赋值
res_H.val=tempvalue
else:
result_head.add_node(ListNode(tempvalue))
tempnode2=tempnode2.next
tempnode1=tempnode1.next
if len1>len2:#是第二个短
longerList=tempnode1
elif len1==len2 and signal_add==True:
result_head.add_node(ListNode(1))
return result_head
elif len2>len1:
longerList=tempnode2
while longerList!=None:
if signal_add==True:
tempvalue=longerList.val+1
else:
tempvalue=longerList.val
if tempvalue>=10:
result_head.add_node(ListNode(tempvalue%10))
signal_add=True
else:
result_head.add_node(ListNode(tempvalue))
signal_add=False
longerList=longerList.next
if signal_add==True:
result_head.add_node(ListNode(1))
return result_head
sum_Nlist(H1,H2).print_order_list()
####上面的方法使用了链表结构,进位通过if else 信号量判断 代码量多 下面仅仅使用ListNode结构,使用add_num(0,1)进位
# Definition for singly-linked list.
# Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
# Output: 7 -> 0 -> 8
# Explanation: 342 + 465 = 807.
class ListNode(object): #定义点
def __init__(self, x):
self.val = x
self.next = None
head1=ListNode(2)
point1=head1
point1.next=ListNode(4)
point1=point1.next
point1.next=ListNode(5)
head2=ListNode(5)
point2=head2
point2.next=ListNode(6)
def sum_list(head1,head2):
add_num=0 #要么加1要么加0 进位
result_head=ListNode(0)
point=result_head
tempnode1=head1
tempnode2=head2
while tempnode1!=None and tempnode2!=None:
value=tempnode1.val+tempnode2.val+add_num
if value>=10:
value=value%10
add_num=1
else:
add_num=0
point.next=ListNode(value)
point=point.next
tempnode1=tempnode1.next
tempnode2=tempnode2.next
if tempnode1!=None:
tempnode=tempnode1
elif tempnode1==None and tempnode2==None:
return result_head
else:
tempnode=tempnode2
while tempnode!=None:
value=tempnode.val+add_num
if value>=10:
value=value%10
add_num=1
else:
add_num=0
point.next=ListNode(value)
point=point.next
tempnode=tempnode.next
return result_head
print(sum_list(head1,head2).next.next.next.val)