Skip to content

206 reverse linked list#8

Open
MA-yo-TA wants to merge 4 commits into
mainfrom
206-Reverse-Linked-List
Open

206 reverse linked list#8
MA-yo-TA wants to merge 4 commits into
mainfrom
206-Reverse-Linked-List

Conversation

@MA-yo-TA

@MA-yo-TA MA-yo-TA commented Jun 9, 2026

Copy link
Copy Markdown
Owner

return current_node
```

( 連結リストを逆向きに繋ぎ変えて、と言われたら普通はまず先にこれを思いつく気がしており、<https://1kohei1.com/leetcode/> を見てカテゴリーごとに問題を解いていることで思考が記載されているカテゴリーに引っ張られ過ぎているかもしれないと思った )

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

最終的にいくつか選択肢が見えてその解き方を選択した根拠がある状態になっていれば良いと思うので、あまり気にしなくても良いのかなと思いました。(ちなみに自分も最初スタックで解きました。)
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.q026dzgqj28b

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.

そうですね、そのための練習ということで気にしすぎずやっていきます。ありがとうございます。

reversed_head = node
node = next_node

return reversed_head

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

読みやすかったです!

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.

ありがとうございます!

checking = checking.next

num_nodes = len(nodes)
if num_nodes <= 1:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

空リストの時だけ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]

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.

ありがとうございます。そのように書いた方が良さそうです。

当初は range() をよくわかっておらず 正のステップで start >= stop (負のステップで start <= stop) の時の挙動を調べずになんとなくエラーになりそうと思って書いたのですが、単に空になるんですね。

メモ:https://docs.python.org/3/library/stdtypes.html#typesseq-range

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.

2 participants