-
Notifications
You must be signed in to change notification settings - Fork 0
2. Add Two Numbers.md #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
new version
class Solution: | ||
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode])-> Optional[ListNode]: | ||
dummy = ListNode() | ||
dummy_node = dummy |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dummy_node って、このコードにおいてはどういう意味でしょうか。
計算済みの桁の最後ということではないでしょうか。上から読んでいく人がなんとなくそれが予期できる名前がいいと思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
最初はdummyという連結リスト、筆算でいう最後の計算結果を書く場所の上を動き、その各位を見ていくという気持ちでdummy_nodeと名付けました。
しかし、ご指摘のように上から読むと分からないため「current_digit_node」に変更しようと思います。ありがとうございます。
2. Add Two Numbers.md
Outdated
total += l2.val | ||
l2 = l2.next | ||
|
||
digid = total % 10 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
digit
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode])-> Optional[ListNode]: | ||
dummy = ListNode() | ||
dummy_node = dummy | ||
total = carry = 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
totalはループの中に入れましょう。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ご指摘ありがとうございます。フィードバックを元に修正してみます!
digit = total % 10 | ||
carry = total // 10 | ||
current_digit_node.next = ListNode(digit) | ||
current_digit_node = current_digit_node.next |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここでcurrent_digit_nodeを更新するまで、current_digit_nodeは「現在計算している桁数を指すノード」ではなく、それより一つ小さい桁のノードとなっているので、自分なら単にnodeにしてしまうかなと思いました。
もしくは、
current_digit_node.next = ListNode()
current_digit_node = current_digit_node.next
をループの最初に書いて、ループの最後でcurrent_digit_node.val = digit
とするかなと思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
返信遅くなり申し訳ありません。
ご指摘の通りcurrent_digit_nodeだと誤解をうみそうですね。
current_digit_nodeだと長いし、提案していただいたnodeで書くようにしてみます!
レビューありがとうございます。
2. Add Two Numbers
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.
次は20. Valid Parentheses