-
Notifications
You must be signed in to change notification settings - Fork 0
206. Reverse Linked List #19
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
huyfififi
left a comment
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.
良いと思います 👍
| rest = head | ||
|
|
||
| while rest: | ||
| rest_tail = rest.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.
restとrest_tailが、どちらも「ひっくり返す残りのNodeの末尾」を保持しているように聞こえるのですが、違うNodeを指すことにわかりづらさを覚えました。
line 142時点ではまだひっくり返していないので、next_rest_tail、line 139をrest_tailとした方がわかりやすいのかな?と思います。
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.
私もこの二つが少しわかりづらいと思いました。
自分はrestをcurrent, rest_tailをsuccessorという名前にしていました。
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.
参考まで、私は
previous = None
node = headで初期化していました。これだと最終的に previous を返すことになるので好みはあると思いますが、個人的には隣接する node たちを処理しているのがわかってよいかなという感じがしています。
| node = head | ||
|
|
||
| while node: | ||
| node_next = node.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.
next_node のほうが語順が自然だと思います。
| visited_nodes.append(node.val) | ||
| node = node.next | ||
|
|
||
| reversed_head = ListNode(visited_nodes.pop(), 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.
dummy_headを作って、visited_nodesからpopする処理は全てwhileループの中でやると、似た処理が1箇所にまとまるので、個人的には好きです。
|
読みやすかったです。 |
206. Reverse Linked List
次回予告: 283. Move Zeroes