Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions 2-add-two-numbers/memo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## step1で考えたこと

和が10以上になった時にどう繰り上げを行うか。

dummyというか結果を格納するList Nodeは必要そう

module 10 でのあまりをNodeの値として使えそう

## step1を振り返って
問題を解く方針はだいたいあっていたが、コードの細かい書き方や、まだLinked Listの書き方がイマイチになっていることに気づいた。

今回の場合、和の一の位の数をNodeの後ろにどんどん追加していく必要があるからtailを使う.
もし、問題の特性が、Nodeを前に前に追加していく場合だと、今度はtailではなくheadを使って操作していく。
28 changes: 28 additions & 0 deletions 2-add-two-numbers/steo2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 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]:
result = ListNode(0)
tail = result
list1 = l1
list2 = l2
carry = 0

while list1 or list2 or carry:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

以下のコメントをご参照ください。
Kazuuuuuuu-u/arai60#2 (comment)
h1rosaka/arai60#47 (comment)

Copy link
Copy Markdown
Owner Author

@05ryt31 05ryt31 Jan 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nodchip
今回の場合だと、

while list1 is not None or list2 is not None or carry is not None:

にした方がより良いという認識で合っていますでしょうか?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

carry は int 型ですので、

while list1 is not None or list2 is not None or carry != 0:

が良いと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そうでした!ありがとうございます!!

x = list1.val if list1 else 0
y = list2.val if list2 else 0

total = x + y + carry
carry = total // 10
digit = total % 10

tail.next = ListNode(digit)
tail = tail.next
if list1:
list1 = list1.next
if list2:
list2 = list2.next
return result.next
25 changes: 25 additions & 0 deletions 2-add-two-numbers/step1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 動かないコード

# 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]:
result = ListNode(0, l1)
list1 = l1.val
list2 = l2.val
res = 0
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

res が residual の略なのか、 result の略なのか、一瞬判断に迷いました。

以下のコメントをご参照ください。
hemispherium/LeetCode_Arai60#10 (comment)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

参考リンクの提供ありがとうございます!
以降は頻繁に使用されるprefix以外の省略は避けていきます。


while list1 or list2:
if res > 10:
res = list1.val + list2.val + 1
else:
res = list1.val + list2.val
if res >= 10:
result.next = res % 10
else: result.next = res
list1 = list1.next
list2 = list2.next
return result.next
30 changes: 30 additions & 0 deletions 2-add-two-numbers/step3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# step2のコードをもう少し簡潔に書いたVersion

# 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()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dummy に番兵ノードを代入しているにもかかわらず、 dummy を先に進めていっている点に違和感を感じました。 dummy の値は動かさず、別のポインターを動かしていったほうが自然に感じられます。

dummy = ListNode()
node = dummy
...
while l1 or l2 or carry:
    ...
    node.next = ListNode(digit)
    node = node.next

return dummy.next

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

修正いたしました!

node = dummy

total = carry = 0

while l1 or l2 or carry:
total = carry

if l1:
total += l1.val
l1 = l1.next
if l2:
total += l2.val
l2 = l2.next

digit = total % 10
carry = total // 10
node.next = ListNode(digit)
node = node.next

return dummy.next