Conversation
| ```python | ||
| class Solution: | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| if head is None: |
There was a problem hiding this comment.
確かにその通りです。
あまり考えずにとりあえずという感じで書いていました。
| ```python | ||
| class Solution: | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| reversed = None |
There was a problem hiding this comment.
reversedはbuiltin functionにあるので命名としては避けるのが無難かなと思います。
https://docs.python.org/3/library/functions.html#reversed
There was a problem hiding this comment.
biltinは関数ですが僕が定義したのは変数なので使っても良いのではないかと思って使ってしまいました。
reversed_でしたら大丈夫でしょうか?
There was a problem hiding this comment.
これとか実行してみると良いと思います。
print(type(reversed))
reversed = 5
print(type(reversed))There was a problem hiding this comment.
あまり詳しくないですが、local name spaceにおいてはreversedも上書きされてしまうっぽいです
https://docs.python.org/3/glossary.html#term-namespace
There was a problem hiding this comment.
プログラムが動くか動かないかという意味で大丈夫か大丈夫ではないかというと大丈夫です。keyword はだめです。(keyword 一覧確認しておきましょう。)
ただ、builtin も後々の書き換えでそれを使いたい人が現れると混乱させるので、書き慣れた人は避けるでしょう。(builtin 一覧も確認しておきましょう。)
There was a problem hiding this comment.
なるほど、ありがとうございます。確認してみたいと思います。
There was a problem hiding this comment.
公式ドキュメントを探して下のように貼り付けるまでを、習慣にするといいでしょう。
https://docs.python.org/3/reference/lexical_analysis.html#keywords
https://docs.python.org/3/library/builtins.html#module-builtins
専門家の振る舞いとして、読んで調べるのは大事です。
| while not_reversed: | ||
| next_temp = not_reversed.next | ||
| not_reversed.next = reversed | ||
|
|
||
| reversed = not_reversed | ||
| not_reversed = next_temp |
There was a problem hiding this comment.
next_tempが思いつきづらいとありましたが、
ここは書こうと思えば、
not_reversed.next, reversed, not_reversed = reversed, not_reversed, not_reversed.next
みたいにnext_tempを使わずに1行で書けます。
たださすがに可読性が悪すぎるので、一時的にnext_tempに退避させていると考えたら
すっと思いつけるのではないかと思いました。
There was a problem hiding this comment.
その考え方はなかったのでアドバイス頂けてありがたいです。
| stack.append(node.val) | ||
| node = node.next | ||
|
|
||
| dummy = ListNode(-1) |
There was a problem hiding this comment.
スタックから初期ノードを持ってくれば、ダミーが不要になってコードがシンプルになりそうですね
There was a problem hiding this comment.
確かにそうですね、思いつきませんでした。ありがとうございます。
| if head is None: | ||
| return None | ||
|
|
||
| stack = list() |
There was a problem hiding this comment.
質問ですが、listを[]ではなく、list()として定義した意図はありますか?
自分も気になって調べましたが、
stack = []の方が関数呼び出しが行われない分自然みたいです。
https://stackoverflow.com/questions/33716401/whats-the-difference-between-list-and
There was a problem hiding this comment.
たしか辞書の初期化も dict() より = {} の方が推奨と聞いたことがあります
https://stackoverflow.com/questions/17097985/dict-vs-in-python-which-is-better
https://switowski.com/blog/dict-function-vs-literal-syntax/
| class Solution: | ||
| # leetcodeはデフォルトの再帰の深さの最大数が55,000のため、今回の制約(最大5,000)だとRecursionErrorは考慮しなくて良い | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| def _reverse_linked_list(head, previous): |
There was a problem hiding this comment.
inner function の名前を _ で始めることは少ないように思います。
_ で始めるのは、メンバ関数のうち non-public なものが多いと思います。
https://peps.python.org/pep-0008/#method-names-and-instance-variables
Use one leading underscore only for non-public methods and instance variables.
問題文
https://leetcode.com/problems/reverse-linked-list/