Skip to content

206. Reverse Linked List#19

Open
skypenguins wants to merge 1 commit into
mainfrom
leetcode/arai60/promlem-206
Open

206. Reverse Linked List#19
skypenguins wants to merge 1 commit into
mainfrom
leetcode/arai60/promlem-206

Conversation

@skypenguins

Copy link
Copy Markdown
Owner

206. Reverse Linked List

次回予告: 283. Move Zeroes

@skypenguins skypenguins self-assigned this Aug 10, 2025

@huyfififi huyfififi left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

良いと思います 👍

Comment thread leetcode/arai60/memo.md
rest = head

while rest:
rest_tail = rest.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.

restrest_tailが、どちらも「ひっくり返す残りのNodeの末尾」を保持しているように聞こえるのですが、違うNodeを指すことにわかりづらさを覚えました。
line 142時点ではまだひっくり返していないので、next_rest_tail、line 139をrest_tailとした方がわかりやすいのかな?と思います。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

私もこの二つが少しわかりづらいと思いました。
自分はrestをcurrent, rest_tailをsuccessorという名前にしていました。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

参考まで、私は

        previous = None
        node = head

で初期化していました。これだと最終的に previous を返すことになるので好みはあると思いますが、個人的には隣接する node たちを処理しているのがわかってよいかなという感じがしています。

Comment thread leetcode/arai60/memo.md
node = head

while node:
node_next = node.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.

next_node のほうが語順が自然だと思います。

Comment thread leetcode/arai60/memo.md
visited_nodes.append(node.val)
node = node.next

reversed_head = ListNode(visited_nodes.pop(), 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.

dummy_headを作って、visited_nodesからpopする処理は全てwhileループの中でやると、似た処理が1箇所にまとまるので、個人的には好きです。

@maeken4

maeken4 commented Aug 24, 2025

Copy link
Copy Markdown

読みやすかったです。

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants