-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path445.Add-Two-Numbers-II-v2.py
39 lines (33 loc) · 1.13 KB
/
445.Add-Two-Numbers-II-v2.py
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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
def reverseList(head):
prev = None
curr = head
while curr:
next_temp = curr.next
# 1 (prev) -> 2 (curr) -> 3 (next)
curr.next = prev
prev = curr
curr = next_temp
return prev
l1 = reverseList(l1)
l2 = reverseList(l2)
head = None
# Carry bit
carry = 0
while l1 or l2 or carry:
l1Val = l1.val if l1 else 0
l2Val = l2.val if l2 else 0
currSum = l1Val + l2Val + carry
newNode = ListNode(currSum % 10)
carry = currSum // 10
newNode.next = head
head = newNode
l1 = l1.next if l1 else None
l2 = l2.next if l2 else None
return head