Skip to content

2. Add Two Numbers.md#6

Open
X-XsleepZzz wants to merge 3 commits into
mainfrom
lilnoahhh-patch-5-1
Open

2. Add Two Numbers.md#6
X-XsleepZzz wants to merge 3 commits into
mainfrom
lilnoahhh-patch-5-1

Conversation

@X-XsleepZzz
Copy link
Copy Markdown
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

Comment thread 2. Add Two Numbers.md
class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode])-> Optional[ListNode]:
dummy = ListNode()
dummy_node = dummy
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Owner Author

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

Comment thread 2. Add Two Numbers.md Outdated
total += l2.val
l2 = l2.next

digid = total % 10
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

digit

Comment thread 2. Add Two Numbers.md
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode])-> Optional[ListNode]:
dummy = ListNode()
dummy_node = dummy
total = carry = 0
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ご指摘ありがとうございます。フィードバックを元に修正してみます!

Comment thread 2. Add Two Numbers.md
digit = total % 10
carry = total // 10
current_digit_node.next = ListNode(digit)
current_digit_node = current_digit_node.next
Copy link
Copy Markdown

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