def list_node_generator(nums): if len(nums) == 0: return None t = ListNode(nums[0]) head = t for i in range(1, len(nums)): t.next = ListNode(nums[i]) t = t.next return head
def add_two_numbers1(l1, l2): l3 = ListNode(0) res = l3 temp = 0 while l1 and l2: l3.val = (l1.val + l2.val + temp) % 10 temp = (l1.val + l2.val + temp) // 10 l1 = l1.next l2 = l2.next if l1 or l2: l3.next = ListNode(0) l3 = l3.next while l1: l3.val = (l1.val + temp) % 10 temp = (l1.val + temp) // 10 l1 = l1.next if l1: l3.next = ListNode(0) l3 = l3.next while l2: l3.val = (l2.val + temp) % 10 temp = (l2.val + temp) // 10 l2 = l2.next if l2: l3.next = ListNode(0) l3 = l3.next if temp > 0: l3.next = ListNode(0) l3.next.val = temp return res
def add_two_numbers2(l1, l2): l3 = ListNode(0) res = l3 carry = 0 while l1 or l2: l3.next = ListNode(0) if l1: carry += l1.val l1 = l1.next if l2: carry += l2.val l2 = l2.next l3.next.val = carry % 10 carry = carry // 10 l3 = l3.next if carry > 0: l3.next = ListNode(0) l3.next.val = carry return res.next
def add_two_numbers3(l1, l2): l3 = ListNode(0) res = l3 carry = 0 while l1 or l2 or carry: l3.next = ListNode(0) if l1: carry += l1.val l1 = l1.next if l2: carry += l2.val l2 = l2.next # 获得商和余数 carry, val = divmod(carry, 10) l3.next.val = val l3 = l3.next return res.next
def insert_at_beginning(self, data): new_node = ListNode(data) new_node.next = self.head self.head = new_node
def test_add_two_numbers(): ''' 思路: 时间复杂度:O(n) ''' def add_two_numbers1(l1, l2): l3 = ListNode(0) res = l3 temp = 0 while l1 and l2: l3.val = (l1.val + l2.val + temp) % 10 temp = (l1.val + l2.val + temp) // 10 l1 = l1.next l2 = l2.next if l1 or l2: l3.next = ListNode(0) l3 = l3.next while l1: l3.val = (l1.val + temp) % 10 temp = (l1.val + temp) // 10 l1 = l1.next if l1: l3.next = ListNode(0) l3 = l3.next while l2: l3.val = (l2.val + temp) % 10 temp = (l2.val + temp) // 10 l2 = l2.next if l2: l3.next = ListNode(0) l3 = l3.next if temp > 0: l3.next = ListNode(0) l3.next.val = temp return res # 方法一优化 def add_two_numbers2(l1, l2): l3 = ListNode(0) res = l3 carry = 0 while l1 or l2: l3.next = ListNode(0) if l1: carry += l1.val l1 = l1.next if l2: carry += l2.val l2 = l2.next l3.next.val = carry % 10 carry = carry // 10 l3 = l3.next if carry > 0: l3.next = ListNode(0) l3.next.val = carry return res.next # 方法2 优化,pythonic def add_two_numbers3(l1, l2): l3 = ListNode(0) res = l3 carry = 0 while l1 or l2 or carry: l3.next = ListNode(0) if l1: carry += l1.val l1 = l1.next if l2: carry += l2.val l2 = l2.next # 获得商和余数 carry, val = divmod(carry, 10) l3.next.val = val l3 = l3.next return res.next print() t1 = ListNode(0) t2 = ListNode(0) test1 = t1 test2 = t2 for i in range(1): t1.val = i + 1 t1.next = ListNode(2) t1 = t1.next for i in range(1): t2.val = 9 t2.next = ListNode(9) t2 = t2.next print(test1) print(test2) print(add_two_numbers3(test1, test2))
sum += carry if carry != 0 else 0 sum += curr1.val if curr1 != None else 0 sum += curr2.val if curr2 != None else 0 if sum >= 10: sum -= 10 carry = 1 else: carry = 0 if total == None: total = ListNode(sum) current = total else: current.next = ListNode(sum) current = current.next curr1 = curr1.next if curr1 != None else None curr2 = curr2.next if curr2 != None else None if carry > 0: current.next = ListNode(carry) return total l1 = ListNode(1) l2 = ListNode(9) l2.next = ListNode(9) s = Solution() total = s.addTwoNumbers(l1, l2)