206. Reverse Linked List - #7
Conversation
| if node.next is None: | ||
| return node, node | ||
| next_node = node.next | ||
| head, tail = reverse_list_helper(next_node) |
There was a problem hiding this comment.
外側の head を上書きしているため、意図がやや分かりづらく感じました。 new_head や reversed_head 等としてはいかがでしょうか?
There was a problem hiding this comment.
ありがとうございます!
順番が逆になっているということをより明示するために、reversed_head という命名に変更しました!
https://github.com/kazizi55/coding-challenges/pull/7/files#diff-f648d8fd3f98d2a609edc8fd5beae741878eb1fd81d035ead5cb5f1a1ce5f210R2-R32
| node, _ = reverse_list_helper(head) | ||
| return node |
There was a problem hiding this comment.
個人的には node -> reversed_head などだとより分かりやすいと思いました。
| node, _ = reverse_list_helper(head) | |
| return node | |
| reversed_head, _ = reverse_list_helper(head) | |
| return reversed_head |
There was a problem hiding this comment.
ありがとうございます!確かに、より意図が伝わる命名にした方が分かりやすいと感じたので、reversed_headに変更しました!
https://github.com/kazizi55/coding-challenges/pull/7/files#diff-f648d8fd3f98d2a609edc8fd5beae741878eb1fd81d035ead5cb5f1a1ce5f210R2-R32
|
|
||
| sentinel = ListNode() | ||
| node = sentinel | ||
| for num in stack[::-1]: |
There was a problem hiding this comment.
組み込み関数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()とすると自然かもしれません。
There was a problem hiding this comment.
ありがとうございます!
どっちでもいいと思いますが、こちらのが自分は好みです。
確かに 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
| def reverse_list_helper(reversed, rest): | ||
| if rest is None: | ||
| return reversed | ||
| next_node = rest.next | ||
| rest.next = reversed |
There was a problem hiding this comment.
変数名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"
}There was a problem hiding this comment.
ありがとうございます!
変数名reversedが組み込み関数reversed()を上書きしてしまうので、避けた方がいいでしょう。
意図せず上書きしてしまっていました。上書きしていることが読み手にとってノイズになって可読性が下がる、そもそも上書きされた組み込み関数が使えなくなるなど、デメリットが多いので避けようと思います。
Step 4 として上書きしない version を追加しました!
https://github.com/kazizi55/coding-challenges/pull/7/files#diff-f648d8fd3f98d2a609edc8fd5beae741878eb1fd81d035ead5cb5f1a1ce5f210R66-R76
| - 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 |
There was a problem hiding this comment.
without tailで書けるなら、with tailで書く必要は特にないと思います。(学習のために両方書いて比較するのはもちろん良いと思います。)
上2つ(WithTail, WithoutTail)と下(PassingRest)は、計算順序の違いによって整理できます。上2つは帰りがけ(bottom-up)で完成、下は行きがけ(top-down)で完成する形ですね。
この話について、以下のDiscordでの議論が自分には参考になったので、よければご覧ください。
https://discord.com/channels/1084280443945353267/1235829049511903273/1238166135489171466
There was a problem hiding this comment.
ありがとうございます。
without tailで書けるなら、with tailで書く必要は特にないと思います。(学習のために両方書いて比較するのはもちろん良いと思います。)
個人的には tail があることで可読性が上がるかなとも思ったのですが (without tail でnode.next.next がどこからともなく出てくる感覚がありました)、確かに必要性はそこまでないかもしれませんね。
上2つ(WithTail, WithoutTail)と下(PassingRest)は、計算順序の違いによって整理できます。上2つは帰りがけ(bottom-up)で完成、下は行きがけ(top-down)で完成する形ですね。
この話について、以下のDiscordでの議論が自分には参考になったので、よければご覧ください。
上下で考え方を変えなければならず、理解に時間がかかっていたのですが、bottom-up と top-down で対比して考えるとスッと頭に入ってきました。ありがとうございます!
|
よく検討されており、読みやすかったと思います。 |
https://leetcode.com/problems/reverse-linked-list/
Next: https://leetcode.com/problems/kth-largest-element-in-a-stream/