206 reverse linked list#8
Open
MA-yo-TA wants to merge 4 commits into
Open
Conversation
wanwan87
reviewed
Jun 16, 2026
| return current_node | ||
| ``` | ||
|
|
||
| ( 連結リストを逆向きに繋ぎ変えて、と言われたら普通はまず先にこれを思いつく気がしており、<https://1kohei1.com/leetcode/> を見てカテゴリーごとに問題を解いていることで思考が記載されているカテゴリーに引っ張られ過ぎているかもしれないと思った ) |
There was a problem hiding this comment.
最終的にいくつか選択肢が見えてその解き方を選択した根拠がある状態になっていれば良いと思うので、あまり気にしなくても良いのかなと思いました。(ちなみに自分も最初スタックで解きました。)
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.q026dzgqj28b
Owner
Author
There was a problem hiding this comment.
そうですね、そのための練習ということで気にしすぎずやっていきます。ありがとうございます。
| reversed_head = node | ||
| node = next_node | ||
|
|
||
| return reversed_head |
| checking = checking.next | ||
|
|
||
| num_nodes = len(nodes) | ||
| if num_nodes <= 1: |
There was a problem hiding this comment.
空リストの時だけnodes[0]で落ちると思うので1を含めず、以下のようなnodesの空判定だけのほうが意図が伝わりやすいかなと思いました。
if not nodes:
return None
num_nodes = len(nodes)
for i in range(num_nodes - 1, 0, -1):
nodes[i].next = nodes[i - 1]
nodes[0].next = None
return nodes[-1]
Owner
Author
There was a problem hiding this comment.
ありがとうございます。そのように書いた方が良さそうです。
当初は range() をよくわかっておらず 正のステップで start >= stop (負のステップで start <= stop) の時の挙動を調べずになんとなくエラーになりそうと思って書いたのですが、単に空になるんですね。
メモ:https://docs.python.org/3/library/stdtypes.html#typesseq-range
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/