Skip to content

Conversation

@skypenguins
Copy link
Owner

206. Reverse Linked List

次回予告: 283. Move Zeroes

@skypenguins skypenguins self-assigned this Aug 10, 2025
Copy link

@huyfififi huyfififi left a comment

Choose a reason for hiding this comment

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

良いと思います 👍

rest = head

while rest:
rest_tail = rest.next

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

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という名前にしていました。

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 たちを処理しているのがわかってよいかなという感じがしています。

node = head

while node:
node_next = node.next
Copy link

Choose a reason for hiding this comment

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

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

visited_nodes.append(node.val)
node = node.next

reversed_head = ListNode(visited_nodes.pop(), None)

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

maeken4 commented Aug 24, 2025

読みやすかったです。

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.

8 participants