Skip to content

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

206. Reverse Linked List #8

wants to merge 3 commits into from

Conversation

rinost081
Copy link
Owner

No description provided.

4分
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
def _reverse_linked_list(head, previous):
Copy link

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 on lines +74 to +76
new_head = self.reverseList(head.next)
head.next.next = head
head.next = None
Copy link

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
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みたいなことを書かなくて良いので読みやすいですね

def _reverse_linked_list(head, previous):
if head is None:
return previous
temp = head.next
Copy link

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:
Copy link

Choose a reason for hiding this comment

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

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

Copy link
Owner Author

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

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

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

@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