Skip to content

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

2. Add Two Numbers.md #6

wants to merge 3 commits into from

Conversation

lilnoahhh
Copy link
Owner

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

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

Choose a reason for hiding this comment

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

dummy_node って、このコードにおいてはどういう意味でしょうか。
計算済みの桁の最後ということではないでしょうか。上から読んでいく人がなんとなくそれが予期できる名前がいいと思います。

Copy link
Owner Author

@lilnoahhh lilnoahhh Jan 14, 2025

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」に変更しようと思います。ありがとうございます。

total += l2.val
l2 = l2.next

digid = total % 10

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

Choose a reason for hiding this comment

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

totalはループの中に入れましょう。

Copy link
Owner Author

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

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とするかなと思います。

Copy link
Owner Author

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で書くようにしてみます!
レビューありがとうございます。

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.

4 participants