206. Reverse Linked List#7
Open
rimokem wants to merge 1 commit into
Open
Conversation
tom4649
reviewed
Mar 29, 2026
Comment on lines
+14
to
+18
| while node is not None: | ||
| next_node = node.next | ||
| node.next = new_head | ||
| new_head = node | ||
| node = next_node |
There was a problem hiding this comment.
多重代入を使えば変数名は減らせそうですが、このままでも良いと思います
node.next, new_head, node = new_head, node, node.next
h-masder
reviewed
Mar 29, 2026
| next_node = node.next | ||
| node.next = new_head | ||
| new_head = node | ||
| node = next_node |
There was a problem hiding this comment.
この解法も再帰の解法も元の入力を書き換えていることは意識しておくとよいかもしれません。
h-masder
reviewed
Mar 29, 2026
| reversed_head, reversed_tail = _reverse_list(node_next) | ||
|
|
||
| reversed_tail.next = node | ||
| return reversed_head, node |
There was a problem hiding this comment.
21行目から26行目は
reversed_head, reversed_tail = _reverse_list(node.next)
node.next = None
reverse_tail.next = node
return reversed_head, nodeのほうがシンプルかなと思いました。
それとreturn文は
return reversed_head, reversed_tail.nextのほうが読みやすいかなと感じます。
Owner
Author
There was a problem hiding this comment.
レビューありがとうございます。
21~26行目については、あらかじめ繋がりを切っておけば_reverse_listで帰って来るリストが純粋にnode以降のリストを反転したものになって分かりやすい、と思って実装しました。
ですが、わざわざ変数を増やしてまでやる事ではなかったかもしれませんね。
return文については、提案していただいた物の方が意図が分かりやすいと感じました。
今後参考にさせていただきます。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
今回の問題
https://leetcode.com/problems/reverse-linked-list/
次回の問題
https://leetcode.com/problems/kth-largest-element-in-a-stream/