题目
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 contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
难度
Medium
方法
对2个链表按位加,求出进位carry
,加到下一位中。当一个链表处理完时,需要继续处理长度更长的另一个链表,处理好进位。注意如果最后的进位为1,则需要新生成一个节点,加到结果链表的结尾
python代码
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:param l1: ListNode
:param l2: ListNode
:return: ListNode
"""
result_list = current = ListNode(0)
carry = 0
while l1 or l2:
l1_val = l1.val if l1 else 0
l2_val = l2.val if l2 else 0
temp_sum = l1_val + l2_val + carry
if temp_sum >= 10:
carry = 1
else:
carry = 0
current.next = ListNode(temp_sum % 10)
current = current.next
if l1:
l1 = l1.next
if l2:
l2 = l2.next
if carry:
current.next = ListNode(1)
return result_list.next
l1 = ListNode(2)
l1.next = ListNode(4)
l1.next.next = ListNode(3)
l2 = ListNode(5)
l2.next = ListNode(6)
l2.next.next = ListNode(4)
result_list = Solution().addTwoNumbers(l1, l2)
assert result_list.val == 7
assert result_list.next.val == 0
assert result_list.next.next.val == 8
l1 = ListNode(1)
l2 = ListNode(9)
l2.next = ListNode(9)
result_list = Solution().addTwoNumbers(l1, l2)
assert result_list.val == 0
assert result_list.next.val == 0
assert result_list.next.next.val == 1
网友评论