Skip to content

# 206. Reverse Linked List#9

Open
liruly wants to merge 2 commits intomainfrom
Arai60/206
Open

# 206. Reverse Linked List#9
liruly wants to merge 2 commits intomainfrom
Arai60/206

Conversation

@liruly
Copy link
Copy Markdown
Owner

@liruly liruly commented Jan 13, 2026

206. Reverse Linked List

## Step1 Stack
- 所要時間: 10min.
- 方針: Stackに格納して最後に取り出す
- stackのカテゴリなので一応やってみる
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

私は、これが stack に入っているのは出題意図とは違うと思いますね。

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.

同意です。一応実装しましたが、実装自体はstackだと非常に楽だと感じました。
逆にスタックを使うべき場面ってあまり直観的に想像がつかないですが、それこそ再帰だとスタックオーバーしそうなとき、とかかなと考えました。

## Step2 ポインタ操作
- 所要時間: 20min.
- 方針: ポインタを用いていい感じにreverseする
- 計算量の観点でも実装のわかりやすさからもこっちが第一選択なように思う
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.

確かに再帰で実装できます。なかなかその発想に至らないのはよくなかったです。
あとで試してみようと思います。
最奥まで探索して逆順に返すのは再帰の得意分野ですね🤔

public:
ListNode* reverseList(ListNode* head) {
ListNode *node = head;
ListNode *prev = nullptr;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

最後に返すオブジェクトが prev なのはあまり直感的ではないと感じました。
c.f., https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.x5w37bodndgj

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.

そもそもnodeとprevという変数名がreverseする問題設定上わかりづらい設定だったかもしれません。
ご指摘ありがとうございます🙏

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