题目描述:
给定两个单向链表,链表的每个节点代表一位数,计算两个数的和
方法分析:
方法一: 整数相加
分别遍历两个链表所代表的值,然后将两个整数加到一起
方法二:链表相加法
对链表中的节点直接进行相加操作,把相加的和存储到新的链表中对应的节点中,同时还要记录节点相加后的进位。
算法性能分析:
这种方法需要对两个链表遍历,因此时间复杂度为o(N),由于计算结果保存在一个新的链表中,空间复杂度为o(N)
代码实现:
1. 带头节点实现
带头结点实现
class LNode:
def __init__(self, item):
self.data = item
self.next = None
def add(h1, h2):
if h1 is None or h1.next is None:
return h2
if h2 is None or h2.next is None:
return h1
c = 0
sum = 0
p1 = h1.next
p2 = h2.next
tmp = None
result_head = LNode(None)
result_head.next = None
p = result_head
while p1 is not None and p2 is not None:
sum = p1.data + p2.data + c
tmp = LNode(sum % 10)
tmp.next = None
c = sum // 10
p.next = tmp
p = tmp
p1 = p1.next
p2 = p2.next
if p1 is None:
while p2 is not None:
sum = p2.data + c
tmp = LNode()
tmp.next = None
tmp.data = sum % 10
c = sum // 10
p.next = tmp
p = tmp
p2 = p2.next
if p2 is None:
while p1 is not None:
sum = p1.data + c
tmp = LNode(sum % 10)
tmp.next = None
c = sum // 10
p.next = tmp
p1 = p1.next
if c == 1:
tmp = LNode(1)
tmp.next = None
p.next = tmp
return result_head
if __name__ == '__main__':
i = 1
head1 = LNode(None)
head1.next = None
head2 = LNode(None)
head2.next = None
tmp = None
cur = head1
add_result = None
while i < 7:
tmp = LNode(i+2)
tmp.next = None
cur.next = tmp
cur = tmp
i += 1
print("i的值为:%d"%i)
i = 9
cur = head2
while i > 4:
tmp = LNode(i)
tmp.next = None
cur.next = tmp
cur = tmp
i -= 1
print("\nhead1")
cur = head1.next
while cur is not None:
print(cur.data)
cur = cur.next
print("\nhead2")
cur = head2.next
while cur is not None:
print(cur.data)
cur = cur.next
add_result = add(head1, head2)
print("\n相加后:")
cur = add_result.next
while cur is not None:
print(cur.data)
cur = cur.next
代码运行结果:
head1
3
4
5
6
7
8
head2
9
8
7
6
5
相加后:
2
3
3
3
3
9
2. 不带头结点
class LNode:
def __init__(self, item):
self.data = item
self.next = None
def add(h1, h2):
if h1 is None:
return h2
if h2 is None:
return h1
c = 0
sum = 0
p1 = h1
p2 = h2
tmp = None
result_head = None
p = result_head
while p1 is not None and p2 is not None:
sum = p1.data + p2.data + c
tmp = LNode(sum % 10)
tmp.next = None
c = sum // 10
if p is not None:
p.next = tmp
p = tmp
else:
result_head = tmp
p = result_head
p1 = p1.next
p2 = p2.next
if p1 is None:
while p2 is not None:
sum = p2.data + c
tmp = LNode()
tmp.next = None
tmp.data = sum % 10
c = sum // 10
p.next = tmp
p = tmp
p2 = p2.next
if p2 is None:
while p1 is not None:
sum = p1.data + c
tmp = LNode(sum % 10)
tmp.next = None
c = sum // 10
p.next = tmp
p1 = p1.next
if c == 1:
tmp = LNode(1)
tmp.next = None
p.next = tmp
return result_head
if __name__ == '__main__':
i = 1
head1 = None
head2 = None
tmp = None
cur = head1
add_result = None
while i < 7:
tmp = LNode(i+2)
tmp.next = None
if cur is not None:
cur.next = tmp
cur = tmp
else:
head1 = tmp
cur = head1
i += 1
print("i的值为:%d"%i)
i = 9
cur = head2
while i > 4:
tmp = LNode(i)
tmp.next = None
if cur is not None:
cur.next = tmp
cur = tmp
else:
head2 = tmp
cur = head2
i -= 1
print("\nhead1")
cur = head1
while cur is not None:
print(cur.data)
cur = cur.next
print("\nhead2")
cur = head2
while cur is not None:
print(cur.data)
cur = cur.next
add_result = add(head1, head2)
print("\n相加后:")
cur = add_result
while cur is not None:
print(cur.data)
cur = cur.next