Skip to content

Create 2. Add Two Numbers.md#7

Open
TrsmYsk wants to merge 1 commit intomainfrom
TrsmYsk-patch-3
Open

Create 2. Add Two Numbers.md#7
TrsmYsk wants to merge 1 commit intomainfrom
TrsmYsk-patch-3

Conversation

@TrsmYsk
Copy link
Owner

@TrsmYsk TrsmYsk commented Oct 11, 2025

今回の問題: 2. Add Two Numbers

次回の問題: 20. Valid Parentheses

Copy link

@nanae772 nanae772 left a comment

Choose a reason for hiding this comment

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

お疲れ様です、全体的に読みやすかったです。

node.next = ListNode(value)
node = node.next
next_carry = total // 10
return add_two_numbers(list1, list2, next_carry, node)

Choose a reason for hiding this comment

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

こちらの場合はreturnした値は必要無いはずなのでreturn無しで終わらせるのが自然かと思いました

Suggested change
return add_two_numbers(list1, list2, next_carry, node)
add_two_numbers(list1, list2, next_carry, node)

Copy link
Owner Author

@TrsmYsk TrsmYsk Oct 12, 2025

Choose a reason for hiding this comment

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

ご指摘のとおり、行きがけの再帰だと文字通り行きがけで各ステップの処理が完結するので、returnは再帰の底でNoneを返せば十分でした。
副作用でリストを作ってるので関数の戻り値はたしかに必要ないですね。

Comment on lines +156 to +157
carry = total // base_number
value = total % base_number

Choose a reason for hiding this comment

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

参考までにこちらはdivmodでまとめることも可能ですね
https://docs.python.org/ja/3/library/functions.html#divmod

Comment on lines +9 to +10
- ループ条件は省略せずに`if l1 is not None`まで書き、ループ内の条件は`if l1`などと省略した。
- ループの部分で条件が明示されてるので条件分岐のところで省略しても可読性は下がらないのではないか、という予想。

Choose a reason for hiding this comment

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

個人的には逆に表記ゆれのように見えてしまうかもしれません。
また厳密にはif l1 is not None:if l1:では違う判定をしているので、判定が異なることに何か意味があるのかと思われるかなと思いました。

Copy link
Owner Author

@TrsmYsk TrsmYsk Oct 12, 2025

Choose a reason for hiding this comment

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

確かにif l1 is not None: はl1とNoneの非同一性のチェックですが、if l1: はl1がTrueがどうかのチェックですね。意識していませんでした。
https://docs.python.org/ja/3.13/library/stdtypes.html#truth-value-testing

carry = total // base_number
value = total % base_number
node.next = ListNode(value)
node = node.next

Choose a reason for hiding this comment

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

全体的に詰まっている感じがあるのでこの後に一行空行を入れてもいいかもしれません。
好みだと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

空行入れたくなる感覚をまだつかみ切れていないので助かります。

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