Skip to content

2-add-two-numbers#13

Merged
05ryt31 merged 3 commits intomainfrom
feat/2
Jan 9, 2026
Merged

2-add-two-numbers#13
05ryt31 merged 3 commits intomainfrom
feat/2

Conversation

@05ryt31
Copy link
Owner

@05ryt31 05ryt31 commented Jan 7, 2026

問題リンク

https://leetcode.com/problems/add-two-numbers/description/

問題文の概要

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

次に解く予定の問題

https://leetcode.com/problems/merge-two-binary-trees/description/

list2 = l2
carry = 0

while list1 or list2 or carry:
Copy link

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
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

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
Owner Author

Choose a reason for hiding this comment

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

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

result = ListNode(0, l1)
list1 = l1.val
list2 = l2.val
res = 0
Copy link

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
Owner Author

Choose a reason for hiding this comment

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

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

# self.next = next
class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode()
Copy link

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
Owner Author

Choose a reason for hiding this comment

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

修正いたしました!

Copy link
Owner Author

@05ryt31 05ryt31 left a comment

Choose a reason for hiding this comment

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

@nodchip
いつもレビューありがとうございます。

# self.next = next
class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode()
Copy link
Owner Author

Choose a reason for hiding this comment

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

修正いたしました!

@05ryt31 05ryt31 merged commit 2e838b1 into main Jan 9, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants