We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 9a39924 commit 5eb3e54Copy full SHA for 5eb3e54
Leetcode/add_two_numbers.py
@@ -0,0 +1,32 @@
1
+# 2. Add Two Numbers
2
+
3
+# Definition for singly-linked list.
4
+# class ListNode:
5
+# def __init__(self, val=0, next=None):
6
+# self.val = val
7
+# self.next = next
8
9
+class Solution:
10
+ def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
11
+ carry = 0
12
+ result = temp = ListNode()
13
14
+ while l1 or l2 or carry:
15
+ l1_val = 0
16
+ l2_val = 0
17
18
+ if l1:
19
+ l1_val = l1.val
20
+ l1 = l1.next
21
+ if l2:
22
+ l2_val = l2.val
23
+ l2 = l2.next
24
25
+ sum_val = l1_val + l2_val + carry
26
27
+ temp.next = ListNode(sum_val % 10)
28
+ temp = temp.next
29
30
+ carry = sum_val // 10
31
32
+ return result.next
0 commit comments