Skip to content

2. add two numbers#4

Open
Rinta-Rinta wants to merge 5 commits intomainfrom
2.-Add-Two-Numbers
Open

2. add two numbers#4
Rinta-Rinta wants to merge 5 commits intomainfrom
2.-Add-Two-Numbers

Conversation

@Rinta-Rinta
Copy link
Copy Markdown
Owner

Comment on lines +30 to +34
if sum == 0:
node = ListNode(0)
return node

dummy = ListNode()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

表記の一貫性がない部分が気になりました。
ListNodeのvalの初期値は0となっているので、ListNode()で良いかなと思いました。

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 = node1_val + node2_val + carry

digit = 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.

今回は10で割ったあまりで、今回の解法でいくと次のfixed_tailのvalだと思うので、それが伝わる命名のfixed_tail_valなどだとより意味が伝わり読み手に親切かなと思いました。

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.

確かにvalをつけた方が意味が分かりやすいですね。勉強になります。

Comment on lines +30 to +34
if sum == 0:
node = ListNode(0)
return node

dummy = ListNode()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

個人的にはこの初期値は使わないのでなんでもいいという意味合いだと思うので、上とはものが違うというのでいいかと思います。

Comment on lines +30 to +32
if sum == 0:
node = ListNode(0)
return node
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この方法だと0を特別扱いする必要がありますね。

# self.val = val
# self.next = next
class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

どうやら両方が None である可能性はこの問題では気にしなくて良いようですが、来たら None が返りますね。

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.

3 participants