Wanwan87 206. reverse linked list#7
Conversation
| while stack: | ||
| reverse_node.next = ListNode(stack.pop()) | ||
| reverse_node = reverse_node.next | ||
| return dummy.next |
There was a problem hiding this comment.
使用する直前に変数を宣言すると目の移動が少なくて良いと思います。
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
node = head
stack = []
while node is not None:
stack.append(node.val)
node = node.next
reverse_node = ListNode()
dummy= reverse_node
while stack:
reverse_node.next = ListNode(stack.pop())
reverse_node = reverse_node.next
return dummy.next| stack.append(node.val) | ||
| node = node.next | ||
| while stack: | ||
| reverse_node.next = ListNode(stack.pop()) |
There was a problem hiding this comment.
stack.pop() のように、値を返すだけでなく状態も変更する副作用のある処理は、引数に直接書かないほうがよさそうです。コードの流れが追いづらくなったり、デバッグしにくくなったりすると思います。
There was a problem hiding this comment.
確かにそうですね、分けて書くようにします。ありがとうございます
| class Solution: | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| node = head | ||
| reverse_node = ListNode() |
There was a problem hiding this comment.
reverseのような動詞は変数名に使うことは多くないと思います。reversed_nodeはどうでしょうか?
There was a problem hiding this comment.
reverseのような動詞は変数名に使うことは多くないと思います。reversed_nodeはどうでしょうか?
あまり意識できていなかったので変数考える際の参考にします
| node = head | ||
| last_fixed_node = None | ||
|
|
||
| while node: |
There was a problem hiding this comment.
ここはwhile node is not Noneと書いたほうがよいと思います。
このあたりご参考にしてみてください。
https://peps.python.org/pep-0008/?utm_source=chatgpt.com#programming-recommendations
There was a problem hiding this comment.
ありがとうございます。明確な使い分けができていなかったため、stackの方の書き方につられていました。
以下の整理をしました。
https://peps.python.org/pep-0008/?utm_source=chatgpt.com#programming-recommendations
while nodeの場合,nodeが「None」と「空」である場合にfalseと判定されてしまうので明示的に is not Noneと書く。
Also, beware of writing if x when you really mean if x is not None – e.g. when testing whether a variable or argument that defaults to None was set to some other value. The other value might have a type (such as a container) that could be false in a boolean context!
リストなどの場合以下のように書く。
For sequences, (strings, lists, tuples), use the fact that empty sequences are false:
Correct:
if not seq:
if seq:
| while node: | ||
| not_fixed_node = node.next | ||
| node.next = last_fixed_node | ||
| lastFixed_node = node |
| - このPRのリンク先の繋ぎ変えの読んでコードを追ってみたが結構短いのに時間がかかってしまった | ||
| - 紙に書いて考えた時にnode.next = lastFixed_nodeでnode.nextを切り離して書いて、nodeを更新していなかった。 | ||
| - 紙の端っこの少スペースで書いていたので、変数の値を同じ個所で斜線で消して上書きしていた。そのため途中で今の状態がよくわからなくなったし、どこでミスしたのかのふりかえりもしにくくなった | ||
| - コードの流れは分かったが、なぜこれで出来るのかが腹落ちしていない感覚 |
There was a problem hiding this comment.
処理を空行を区切るとイメージしやすくなるかもしれません。また、コメントアウトも有効です。
while node:
not_fixed_node = node.next
# リンクノードをつなぎ変える
node.next = last_fixed_node
last_fixed_node = node
node = not_fixed_nodeThere was a problem hiding this comment.
ありがとうございます、紙面に書いたときは変数の動きなどを見ていて、言語化をしていなかったので、コメントアウトなどで言語化もしてみようと思います
この問題:https://leetcode.com/problems/reverse-linked-list/description/
次の問題:https://leetcode.com/problems/kth-largest-element-in-a-stream/description/