Skip to main content

Add Two Numbers II

描述​

You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first 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: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7

Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.

分析​

先把两个单链表反转,就可以直接复用 Add Two Numbers 的代码了。

代码​

# Add Two Numbers II
# Time Complexity: O(m+n), Time Complexity: O(1)
class Solution:
def addTwoNumbers(self, l1, l2):
l1 = self.reverseList(l1)
l2 = self.reverseList(l2)
result = self.addTwoNumbersI(l1, l2)
return self.reverseList(result)

@staticmethod
def addTwoNumbersI(l1, l2):
dummy = ListNode(-1) # 头节点
carry = 0
prev = dummy
pa, pb = l1, l2
while pa is not None or pb is not None:
ai = 0 if pa is None else pa.val
bi = 0 if pb is None else pb.val
value = (ai + bi + carry) % 10
carry = (ai + bi + carry) // 10
prev.next = ListNode(value) # 尾插法
pa = None if pa is None else pa.next
pb = None if pb is None else pb.next
prev = prev.next
if carry > 0:
prev.next = ListNode(carry)
return dummy.next

@staticmethod
def reverseList(head):
p = None
q = head
while q is not None:
tmp = q.next
q.next = p
p = q
q = tmp
return p

相关题目​