Skip to content

206. Reverse Linked List#7

Open
rimokem wants to merge 1 commit into
mainfrom
0206-reverse-linked-list
Open

206. Reverse Linked List#7
rimokem wants to merge 1 commit into
mainfrom
0206-reverse-linked-list

Conversation

@rimokem
Copy link
Copy Markdown
Owner

@rimokem rimokem commented Mar 28, 2026

Comment thread 0206/memo.md
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
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

多重代入を使えば変数名は減らせそうですが、このままでも良いと思います

node.next, new_head, node = new_head, node, node.next

Comment thread 0206/memo.md
next_node = node.next
node.next = new_head
new_head = node
node = next_node
Copy link
Copy Markdown

@h-masder h-masder Mar 29, 2026

Choose a reason for hiding this comment

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

この解法も再帰の解法も元の入力を書き換えていることは意識しておくとよいかもしれません。

Comment thread 0206/step3.py
reversed_head, reversed_tail = _reverse_list(node_next)

reversed_tail.next = node
return reversed_head, node
Copy link
Copy Markdown

@h-masder h-masder Mar 29, 2026

Choose a reason for hiding this comment

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

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

のほうが読みやすいかなと感じます。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

レビューありがとうございます。

21~26行目については、あらかじめ繋がりを切っておけば_reverse_listで帰って来るリストが純粋にnode以降のリストを反転したものになって分かりやすい、と思って実装しました。
ですが、わざわざ変数を増やしてまでやる事ではなかったかもしれませんね。

return文については、提案していただいた物の方が意図が分かりやすいと感じました。

今後参考にさせていただきます。

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.

3 participants