-
Notifications
You must be signed in to change notification settings - Fork 0
Wanwan87 206. reverse linked list #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| # step1 | ||
| - 入力をスタックに入れていって、popしていけば良さそう | ||
|
|
||
| ```python | ||
| # Definition for singly-linked list. | ||
| # class ListNode: | ||
| # def __init__(self, val=0, next=None): | ||
| # self.val = val | ||
| # self.next = next | ||
| class Solution: | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| node = head | ||
| reverse_node = ListNode() | ||
| dummy= reverse_node | ||
| stack = [] | ||
| while node is not None: | ||
| stack.append(node.val) | ||
| node = node.next | ||
| while stack: | ||
| reverse_node.next = ListNode(stack.pop()) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. stack.pop() のように、値を返すだけでなく状態も変更する副作用のある処理は、引数に直接書かないほうがよさそうです。コードの流れが追いづらくなったり、デバッグしにくくなったりすると思います。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 確かにそうですね、分けて書くようにします。ありがとうございます |
||
| reverse_node = reverse_node.next | ||
| return dummy.next | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 使用する直前に変数を宣言すると目の移動が少なくて良いと思います。 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 |
||
| ``` | ||
|
|
||
| # step2 | ||
| - PRなどを見る | ||
| - スタックではなく繋ぎ変える方法もある(出題意図としてはこっち?) | ||
| - https://github.com/olsen-blue/Arai60/pull/7/changes#r1904334523 | ||
| - このPRのリンク先の繋ぎ変えの読んでコードを追ってみたが結構短いのに時間がかかってしまった | ||
| - 紙に書いて考えた時にnode.next = lastFixed_nodeでnode.nextを切り離して書いて、nodeを更新していなかった。 | ||
| - 紙の端っこの少スペースで書いていたので、変数の値を同じ個所で斜線で消して上書きしていた。そのため途中で今の状態がよくわからなくなったし、どこでミスしたのかのふりかえりもしにくくなった | ||
| - コードの流れは分かったが、なぜこれで出来るのかが腹落ちしていない感覚 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 処理を空行を区切るとイメージしやすくなるかもしれません。また、コメントアウトも有効です。 while node:
not_fixed_node = node.next
# リンクノードをつなぎ変える
node.next = last_fixed_node
last_fixed_node = node
node = not_fixed_node
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ありがとうございます、紙面に書いたときは変数の動きなどを見ていて、言語化をしていなかったので、コメントアウトなどで言語化もしてみようと思います |
||
| - https://discord.com/channels/1084280443945353267/1355246975309844550/1355898121460252784 | ||
| - これの3のイメージかな、コードから具体的なイメージが書けるようにしたい | ||
| - https://peps.python.org/pep-0008/#descriptive-naming-styles | ||
| - PEP8では関数名と変数名は小文字で単語間はアンダースコアでつなげる | ||
| - 再帰のやり方もある、が今回は軽く目を通す程度にしておいて次に進む | ||
| - https://discord.com/channels/1084280443945353267/1231966485610758196/1239417493211320382 | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| node = head | ||
| last_fixed_node = None | ||
|
|
||
| while node: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ここはwhile node is not Noneと書いたほうがよいと思います。 https://peps.python.org/pep-0008/?utm_source=chatgpt.com#programming-recommendations
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ありがとうございます。明確な使い分けができていなかったため、stackの方の書き方につられていました。 以下の整理をしました。 https://peps.python.org/pep-0008/?utm_source=chatgpt.com#programming-recommendations while nodeの場合,nodeが「None」と「空」である場合にfalseと判定されてしまうので明示的に is not Noneと書く。
リストなどの場合以下のように書く。
|
||
| not_fixed_node = node.next | ||
| node.next = last_fixed_node | ||
| lastFixed_node = node | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ここは、last_fixed_nodeかなと思います |
||
| node = notFixed_node | ||
|
|
||
| return lastFixed_node | ||
| ``` | ||
|
|
||
| # step3 | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| node = head | ||
| last_fixed_node = None | ||
| while node: | ||
| not_fixed_node = node.next | ||
| node.next = last_fixed_node | ||
| last_fixed_node = node | ||
| node = not_fixed_node | ||
| return last_fixed_node | ||
| ``` | ||
| ```python | ||
| class Solution: | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| node = head | ||
| last_fixed_node = None | ||
| while node: | ||
| not_fixed_node = node.next | ||
| node.next = last_fixed_node | ||
| last_fixed_node = node | ||
| node = not_fixed_node | ||
| return last_fixed_node | ||
| ``` | ||
| ```python | ||
| class Solution: | ||
| def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| node = head | ||
| last_fixed_node = None | ||
| while node: | ||
| not_fixed_node = node.next | ||
| node.next = last_fixed_node | ||
| last_fixed_node = node | ||
| node = not_fixed_node | ||
| return last_fixed_node | ||
| ``` | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reverseのような動詞は変数名に使うことは多くないと思います。reversed_nodeはどうでしょうか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あまり意識できていなかったので変数考える際の参考にします