Skip to content

206. Reverse Linked List#8

Open
rinost081 wants to merge 3 commits intomainfrom
LeetCode206
Open

206. Reverse Linked List#8
rinost081 wants to merge 3 commits intomainfrom
LeetCode206

Conversation

@rinost081
Copy link
Copy Markdown
Owner

No description provided.

Comment thread lc206.py
4分
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
def _reverse_linked_list(head, previous):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inner function の関数名の先頭には _ を付けなくてよいみたいです。

katataku/leetcode#4 (comment)

@rinost081 rinost081 changed the title Leet code206 206. Reverse Linked List Dec 5, 2024
Comment thread lc206.py
Comment on lines +74 to +76
new_head = self.reverseList(head.next)
head.next.next = head
head.next = None
Copy link
Copy Markdown

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

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

確かにtailを使って書いた方がhead.next.nextみたいなことを書かなくて良いので読みやすいですね

Comment thread lc206.py
def _reverse_linked_list(head, previous):
if head is None:
return previous
temp = head.next
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

関数にも渡す値となると、命名はtempじゃないほうがよいと思いました。
tempの使いどきは、変数のswapのようなレベルで値を一時保存するときだけの印象があります。

Comment thread lc206.py

この問題の解き方は以下の方法がありそう
1. スタックで繋ぎ変え
class Solution:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

レビューわーにとって読みやすくるため、シンタックスハイライトが有効になるような書き方をしていただけるとありがたいです。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

承知しました、次回以降別の書き方にしてみます

Comment thread lc206.py
if not stack:
return None
reversed_head = stack.pop()
current = reversed_head
Copy link
Copy Markdown

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_originalnode_in_reversedなどを使ってました。

Comment thread lc206.py
1. スタックで繋ぎ変え
class Solution:
def reverseList(self, head: [Optional[ListNode]) -> Optional[ListNode]:
stack = []
Copy link
Copy Markdown

@ichika0615 ichika0615 Dec 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

データ構造の名前をそのまま命名に使うと読みにくくなる気がしました。found_nodesとかnodesとかいいかもです

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants