Conversation
nodchip
reviewed
Jan 8, 2026
| list2 = l2 | ||
| carry = 0 | ||
|
|
||
| while list1 or list2 or carry: |
There was a problem hiding this comment.
以下のコメントをご参照ください。
Kazuuuuuuu-u/arai60#2 (comment)
h1rosaka/arai60#47 (comment)
Owner
Author
There was a problem hiding this comment.
@nodchip
今回の場合だと、
while list1 is not None or list2 is not None or carry is not None:にした方がより良いという認識で合っていますでしょうか?
There was a problem hiding this comment.
carry は int 型ですので、
while list1 is not None or list2 is not None or carry != 0:が良いと思います。
| result = ListNode(0, l1) | ||
| list1 = l1.val | ||
| list2 = l2.val | ||
| res = 0 |
There was a problem hiding this comment.
res が residual の略なのか、 result の略なのか、一瞬判断に迷いました。
以下のコメントをご参照ください。
hemispherium/LeetCode_Arai60#10 (comment)
Owner
Author
There was a problem hiding this comment.
参考リンクの提供ありがとうございます!
以降は頻繁に使用されるprefix以外の省略は避けていきます。
| # self.next = next | ||
| class Solution: | ||
| def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: | ||
| dummy = ListNode() |
There was a problem hiding this comment.
dummy に番兵ノードを代入しているにもかかわらず、 dummy を先に進めていっている点に違和感を感じました。 dummy の値は動かさず、別のポインターを動かしていったほうが自然に感じられます。
dummy = ListNode()
node = dummy
...
while l1 or l2 or carry:
...
node.next = ListNode(digit)
node = node.next
return dummy.next
05ryt31
commented
Jan 9, 2026
| # self.next = next | ||
| class Solution: | ||
| def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: | ||
| dummy = ListNode() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
問題リンク
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/