From e43efcf55d807946b54d262971c7a2d784da6e88 Mon Sep 17 00:00:00 2001 From: wanwan87 Date: Thu, 30 Apr 2026 22:49:50 +0900 Subject: [PATCH 1/4] Create 2. Add Two Numbers.md --- 2. Add Two Numbers.md | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 2. Add Two Numbers.md diff --git a/2. Add Two Numbers.md b/2. Add Two Numbers.md new file mode 100644 index 0000000..c1d6eb9 --- /dev/null +++ b/2. Add Two Numbers.md @@ -0,0 +1,41 @@ +# step1 +- 前回反省を踏まえてとりあえず紙に書きだしてみて全体の流れを抑えた +- l1とl2をループで回して加算をする +- 加算の結果10以上の場合は繰り上げが必要 + - 繰り上げフラグを立ててみる + - 繰り上げフラグが立っている場合は加算の結果に+1をする +- 検証が必要なこと + - 一時的にlistで計算結果を入れておいて、最後にListNodeに詰めていきたい。 + - ListNodeにどう詰めるか + - listに一時的に入れておく必要があるのか + +```python +# 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]: + l3 = list() + add_result = ListNode() + carry = 0 + while l1 is not None and l2 is not None: + tmp = l1.val+l2.val + if tmp >= 10: + l3.append(tmp % 10) + carry = 1 + elif carry == 1: + l3.append(tmp + 1) + else: + l3.append(tmp) + l1 = l1.next + l2 = l2.next + # ここ + for num,index in enumerate(l3): + add_result.val = index + add_result.next = + return add_result + +``` + From dcb613c63642e61b703cf1c64e4df61c3ecd9024 Mon Sep 17 00:00:00 2001 From: wanwan87 Date: Wed, 6 May 2026 22:58:15 +0900 Subject: [PATCH 2/4] Update 2. Add Two Numbers.md --- 2. Add Two Numbers.md | 67 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 5 deletions(-) diff --git a/2. Add Two Numbers.md b/2. Add Two Numbers.md index c1d6eb9..9c05942 100644 --- a/2. Add Two Numbers.md +++ b/2. Add Two Numbers.md @@ -31,11 +31,68 @@ class Solution: l3.append(tmp) l1 = l1.next l2 = l2.next - # ここ - for num,index in enumerate(l3): - add_result.val = index - add_result.next = + for i in l3: + # ここでListNodeに詰め込む処理を書きたい。 return add_result +``` + +- l1=[9,9,9,9,9,9,9],l2=[9,9,9,9]のときに5ループ目で止まっている + - l1とl2が同じ長さのListNodeではないため。l2が先にNoneにwhileの条件で止まってしまっている。 + - whileの条件を or にして、l1とl2がNoneの場合に、l1とl2の加算で0を加算するような配慮が必要になりそう +- ListNodeの処理部分も思いつかなかったので答えや他の方のPRを見る + +#step2 +- コメント集結構量があったので理解できる範囲で読む。 + - https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.lxzt19oefrb8 +- https://github.com/hiro111208/leetcode/pull/5/changes + - ifの条件がis not Noneに見慣れているので個人的にはそっちの書き方が好み + - whileの中でListNodeに詰めていって、dummyをreturnするのでもいいのか + - //で商を繰り上げている +- https://github.com/rimokem/arai60/pull/5/changes  + - if文が3項演算子 + - https://note.nkmk.me/python-if-conditional-expressions/ + - divmodという関数を使っている。割り算の商と余りを同時に取得できる + - 繰り上げを真偽値よりも数値で持つ方が3つ以上のリストの足し算にも対応できる +- https://github.com/Rinta-Rinta/LeetCode_arai60/pull/4/changes + - whileの条件で繰り上げがないことを含める必要がある +# step3 +```python +# 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]: + dummy = ListNode() + node = dummy + carry = 0 + while l1 is not None or l2 is not None or carry !=0: + if l1 is not None: + value1 = l1.val + else: + value1 = 0 + if l2 is not None: + value2 = l2.val + else: + value2 = 0 + tmp = value1 + value2 + carry + carry = tmp // 10 + digit = tmp % 10 + node.next = ListNode(digit) + node = node.next + + if l1 is not None: + l1=l1.next + else: + None + if l2 is not None: + l2=l2.next + else: + None + return dummy.next ``` - + + From 12616f846abac0d6df35cfaa5069fda27d3cce4a Mon Sep 17 00:00:00 2001 From: wanwan87 Date: Wed, 6 May 2026 23:03:49 +0900 Subject: [PATCH 3/4] Update 2. Add Two Numbers.md --- 2. Add Two Numbers.md | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/2. Add Two Numbers.md b/2. Add Two Numbers.md index 9c05942..5c97e54 100644 --- a/2. Add Two Numbers.md +++ b/2. Add Two Numbers.md @@ -2,10 +2,10 @@ - 前回反省を踏まえてとりあえず紙に書きだしてみて全体の流れを抑えた - l1とl2をループで回して加算をする - 加算の結果10以上の場合は繰り上げが必要 - - 繰り上げフラグを立ててみる + - 繰り上げフラグを立ててみる - 繰り上げフラグが立っている場合は加算の結果に+1をする - 検証が必要なこと - - 一時的にlistで計算結果を入れておいて、最後にListNodeに詰めていきたい。 + - 一時的にlistで計算結果を入れておいて、最後にListNodeに詰めていきたい。 - ListNodeにどう詰めるか - listに一時的に入れておく必要があるのか @@ -37,24 +37,24 @@ class Solution: ``` - l1=[9,9,9,9,9,9,9],l2=[9,9,9,9]のときに5ループ目で止まっている - - l1とl2が同じ長さのListNodeではないため。l2が先にNoneにwhileの条件で止まってしまっている。 - - whileの条件を or にして、l1とl2がNoneの場合に、l1とl2の加算で0を加算するような配慮が必要になりそう + - l1とl2が同じ長さのListNodeではないため。l2が先にNoneにwhileの条件で止まってしまっている。 + - whileの条件を or にして、l1とl2がNoneの場合に、l1とl2の加算で0を加算するような配慮が必要になりそう - ListNodeの処理部分も思いつかなかったので答えや他の方のPRを見る -#step2 +# step2 - コメント集結構量があったので理解できる範囲で読む。 - - https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.lxzt19oefrb8 + - https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.lxzt19oefrb8 - https://github.com/hiro111208/leetcode/pull/5/changes - - ifの条件がis not Noneに見慣れているので個人的にはそっちの書き方が好み - - whileの中でListNodeに詰めていって、dummyをreturnするのでもいいのか - - //で商を繰り上げている -- https://github.com/rimokem/arai60/pull/5/changes  - - if文が3項演算子 + - ifの条件がis not Noneに見慣れているので個人的にはそっちの書き方が好み + - whileの中でListNodeに詰めていって、dummyをreturnするのでもいいのか + - //で商を繰り上げている +- https://github.com/rimokem/arai60/pull/5/changes + - if文が3項演算子 - https://note.nkmk.me/python-if-conditional-expressions/ - - divmodという関数を使っている。割り算の商と余りを同時に取得できる - - 繰り上げを真偽値よりも数値で持つ方が3つ以上のリストの足し算にも対応できる + - divmodという関数を使っている。割り算の商と余りを同時に取得できる + - 繰り上げを真偽値よりも数値で持つ方が3つ以上のリストの足し算にも対応できる - https://github.com/Rinta-Rinta/LeetCode_arai60/pull/4/changes - - whileの条件で繰り上げがないことを含める必要がある + - whileの条件で繰り上げがないことを含める必要がある # step3 ```python From d62d84adbd252aa88852ab7348e109ee80f5336c Mon Sep 17 00:00:00 2001 From: wanwan87 Date: Thu, 7 May 2026 23:17:59 +0900 Subject: [PATCH 4/4] Update 2. Add Two Numbers.md --- 2. Add Two Numbers.md | 70 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 65 insertions(+), 5 deletions(-) diff --git a/2. Add Two Numbers.md b/2. Add Two Numbers.md index 5c97e54..5deda52 100644 --- a/2. Add Two Numbers.md +++ b/2. Add Two Numbers.md @@ -44,13 +44,14 @@ class Solution: # step2 - コメント集結構量があったので理解できる範囲で読む。 - https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.lxzt19oefrb8 + - - https://github.com/hiro111208/leetcode/pull/5/changes - ifの条件がis not Noneに見慣れているので個人的にはそっちの書き方が好み - whileの中でListNodeに詰めていって、dummyをreturnするのでもいいのか - //で商を繰り上げている - https://github.com/rimokem/arai60/pull/5/changes - if文が3項演算子 - - https://note.nkmk.me/python-if-conditional-expressions/ + - https://note.nkmk.me/python-if-conditional-expressions/ - divmodという関数を使っている。割り算の商と余りを同時に取得できる - 繰り上げを真偽値よりも数値で持つ方が3つ以上のリストの足し算にも対応できる - https://github.com/Rinta-Rinta/LeetCode_arai60/pull/4/changes @@ -83,15 +84,74 @@ class Solution: digit = tmp % 10 node.next = ListNode(digit) node = node.next - if l1 is not None: l1=l1.next - else: - None if l2 is not None: l2=l2.next + return dummy.next +``` + +```python +# 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]: + dummy = ListNode() + node = dummy + carry = 0 + while l1 is not None or l2 is not None or carry != 0: + if l1 is not None: + value1 = l1.val + else: + value1 = 0 + if l2 is not None: + value2 = l2.val + else: + value2 = 0 + tmp = value1 + value2 + carry + carry = tmp // 10 + digit = tmp % 10 + node.next = ListNode(digit) + node = node.next + if l1 is not None: + l1 = l1.next + if l2 is not None: + l2 = l2.next + return dummy.next +``` + +```python +# 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]: + dummy = ListNode() + node = dummy + carry = 0 + while l1 is not None or l2 is not None or carry != 0: + if l1 is not None: + value1 = l1.val else: - None + value1 = 0 + if l2 is not None: + value2 = l2.val + else: + value2 = 0 + tmp = value1 + value2 + carry + carry = tmp // 10 + digit = tmp % 10 + node.next = ListNode(digit) + node = node.next + if l1 is not None: + l1 = l1.next + if l2 is not None: + l2 = l2.next return dummy.next ```