-
Notifications
You must be signed in to change notification settings - Fork 0
206. Reverse Linked List #8
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?
Conversation
4分 | ||
class Solution: | ||
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
def _reverse_linked_list(head, previous): |
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.
inner function の関数名の先頭には _ を付けなくてよいみたいです。
new_head = self.reverseList(head.next) | ||
head.next.next = head | ||
head.next = None |
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.
これ、私は先に一回切断したほうが理解はしやすいと思います。
tail = head.next
head.next = None
new_head = self.reverseList(tail)
# tail is now the last node of the reversed list.
tail.next = head
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.
確かにtailを使って書いた方がhead.next.nextみたいなことを書かなくて良いので読みやすいですね
def _reverse_linked_list(head, previous): | ||
if head is None: | ||
return previous | ||
temp = head.next |
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.
関数にも渡す値となると、命名はtempじゃないほうがよいと思いました。
tempの使いどきは、変数のswapのようなレベルで値を一時保存するときだけの印象があります。
|
||
この問題の解き方は以下の方法がありそう | ||
1. スタックで繋ぎ変え | ||
class Solution: |
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.
レビューわーにとって読みやすくるため、シンタックスハイライトが有効になるような書き方をしていただけるとありがたいです。
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.
承知しました、次回以降別の書き方にしてみます
if not stack: | ||
return None | ||
reversed_head = stack.pop() | ||
current = reversed_head |
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.
current
が違う文脈で2回登場するのが読みづらいかもしれません。最初のcurrent
はオリジナルの連結リストを走査するのに使って、次のcurrent
はリバースされた連結リストのノードのことを指していると思います。私はnode_in_original
、node_in_reversed
などを使ってました。
1. スタックで繋ぎ変え | ||
class Solution: | ||
def reverseList(self, head: [Optional[ListNode]) -> Optional[ListNode]: | ||
stack = [] |
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.
データ構造の名前をそのまま命名に使うと読みにくくなる気がしました。found_nodes
とかnodes
とかいいかもです
No description provided.