Skip to content

206. Reverse Linked List - #7

Open
kazizi55 wants to merge 2 commits into
mainfrom
206-reverse-linked-list
Open

206. Reverse Linked List#7
kazizi55 wants to merge 2 commits into
mainfrom
206-reverse-linked-list

Conversation

@kazizi55

@kazizi55 kazizi55 commented Nov 2, 2025

Copy link
Copy Markdown
Owner

@kazizi55
kazizi55 marked this pull request as ready for review November 2, 2025 07:29
if node.next is None:
return node, node
next_node = node.next
head, tail = reverse_list_helper(next_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.

外側の head を上書きしているため、意図がやや分かりづらく感じました。 new_head や reversed_head 等としてはいかがでしょうか?

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.

ありがとうございます!
順番が逆になっているということをより明示するために、reversed_head という命名に変更しました!
https://github.com/kazizi55/coding-challenges/pull/7/files#diff-f648d8fd3f98d2a609edc8fd5beae741878eb1fd81d035ead5cb5f1a1ce5f210R2-R32

Comment on lines +13 to +14
node, _ = reverse_list_helper(head)
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.

個人的には node -> reversed_head などだとより分かりやすいと思いました。

Suggested change
node, _ = reverse_list_helper(head)
return node
reversed_head, _ = reverse_list_helper(head)
return reversed_head

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.

ありがとうございます!確かに、より意図が伝わる命名にした方が分かりやすいと感じたので、reversed_headに変更しました!
https://github.com/kazizi55/coding-challenges/pull/7/files#diff-f648d8fd3f98d2a609edc8fd5beae741878eb1fd81d035ead5cb5f1a1ce5f210R2-R32


sentinel = ListNode()
node = sentinel
for num in stack[::-1]:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

組み込み関数reversed()を使って、

for num in reversed(stack):

と書くこともできます。どっちでもいいと思いますが、こちらのが自分は好みです。

https://docs.python.org/ja/3/library/functions.html#reversed

もしくは、stack、つまりLast In First Outがappend, popに対応することを活かして、

while stack:
    num = stack.pop()

とすると自然かもしれません。

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.

ありがとうございます!

どっちでもいいと思いますが、こちらのが自分は好みです。

確かに reversed() を使う方が、list の順番が逆になるのがより直感的ですね。

もしくは、stack、つまりLast In First Outがappend, popに対応することを活かして、...とすると自然かもしれません。

かなり自然に感じました。stack という変数名を使うなら append と合わせて pop も使うことに一貫性を感じますね。

以上2パターンを Step 4 として追加しました!
https://github.com/kazizi55/coding-challenges/pull/7/files#diff-f648d8fd3f98d2a609edc8fd5beae741878eb1fd81d035ead5cb5f1a1ce5f210R34-R64

Comment on lines +33 to +37
def reverse_list_helper(reversed, rest):
if rest is None:
return reversed
next_node = rest.next
rest.next = reversed

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

変数名reversedが組み込み関数reversed()を上書きしてしまうので、避けた方がいいでしょう。

https://docs.python.org/ja/3/library/functions.html#reversed

docs.python.orgを何度も参照しているうちに自然と避けられるようになると思います。

以下、全部は網羅していないですが、他にはこんなのもあります。

FORBIDDEN = {
    # 他言語(C/C++/Java/TS/Go/Python)での予約語になりがち
    "char","int","long","short","float","double","bool","string",
    "class","struct","union","enum","interface","implements","extends",
    "public","private","protected","package","import","export","module",
    "new","delete","this","super","const","var","let","yield","lambda",
    "async","await","from","in","is","not","or","and","record","match","type",
    # Python 組み込み
    "list","dict","set","tuple","id","sum","max","min","any","all","input",
    "bytes","map","filter","range","str","type"
}

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.

ありがとうございます!

変数名reversedが組み込み関数reversed()を上書きしてしまうので、避けた方がいいでしょう。

意図せず上書きしてしまっていました。上書きしていることが読み手にとってノイズになって可読性が下がる、そもそも上書きされた組み込み関数が使えなくなるなど、デメリットが多いので避けようと思います。

Step 4 として上書きしない version を追加しました!
https://github.com/kazizi55/coding-challenges/pull/7/files#diff-f648d8fd3f98d2a609edc8fd5beae741878eb1fd81d035ead5cb5f1a1ce5f210R66-R76

Comment on lines +47 to +53
- RecursiveSolutionWithTail
- https://discord.com/channels/1084280443945353267/1231966485610758196/1239417493211320382
- https://github.com/TORUS0818/leetcode/pull/9/files#r1598170316
- RecursiveSolutionWithoutTail
- https://github.com/goto-untrapped/Arai60/pull/27#discussion_r1641596128
- RecursiveSolutionPassingRest
- https://github.com/goto-untrapped/Arai60/pull/27#discussion_r1641789968

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

without tailで書けるなら、with tailで書く必要は特にないと思います。(学習のために両方書いて比較するのはもちろん良いと思います。)

上2つ(WithTail, WithoutTail)と下(PassingRest)は、計算順序の違いによって整理できます。上2つは帰りがけ(bottom-up)で完成、下は行きがけ(top-down)で完成する形ですね。

この話について、以下のDiscordでの議論が自分には参考になったので、よければご覧ください。

https://discord.com/channels/1084280443945353267/1235829049511903273/1238166135489171466

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.

ありがとうございます。

without tailで書けるなら、with tailで書く必要は特にないと思います。(学習のために両方書いて比較するのはもちろん良いと思います。)

個人的には tail があることで可読性が上がるかなとも思ったのですが (without tail でnode.next.next がどこからともなく出てくる感覚がありました)、確かに必要性はそこまでないかもしれませんね。

上2つ(WithTail, WithoutTail)と下(PassingRest)は、計算順序の違いによって整理できます。上2つは帰りがけ(bottom-up)で完成、下は行きがけ(top-down)で完成する形ですね。
この話について、以下のDiscordでの議論が自分には参考になったので、よければご覧ください。

上下で考え方を変えなければならず、理解に時間がかかっていたのですが、bottom-up と top-down で対比して考えるとスッと頭に入ってきました。ありがとうございます!

@TrsmYsk

TrsmYsk commented Nov 3, 2025

Copy link
Copy Markdown

よく検討されており、読みやすかったと思います。

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.

5 participants