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 39af035 commit c233167Copy full SHA for c233167
14. Questions/leetcode 02 - add two numbers.py
@@ -0,0 +1,38 @@
1
+# add two numbers | leetcode 02 | https://leetcode.com/problems/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: list[ListNode], l2: list[ListNode]) -> list[ListNode]:
11
+ res = ListNode()
12
+ head = res
13
14
+ while l1 != None or l2 != None:
15
+ if l1 == None:
16
+ this_val = res.val + l2.val
17
+ l2 = l2.next
18
+ elif l2 == None:
19
+ this_val = res.val + l1.val
20
+ l1 = l1.next
21
+ else:
22
+ this_val = res.val + l1.val + l2.val
23
+ l1, l2 = l1.next, l2.next
24
25
+ this_digit = this_val % 10
26
+ next_digit = this_val // 10
27
28
+ res.val = this_digit
29
+ if l1 != None or l2 != None:
30
+ res.next = ListNode(next_digit)
31
+ res = res.next
32
33
+ if next_digit > 0:
34
35
36
37
+ return head
38
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments