Skip to content

Conversation

@Ryotaro25
Copy link
Owner

問題へのリンク
https://leetcode.com/problems/reverse-linked-list-ii/description/

問題文(プレミアムの場合)

備考

次に解く問題の予告
ZigZag Conversion

フォルダ構成
LeetCodeの問題ごとにフォルダを作成します。
フォルダ内は、step1.cpp、step2_1.cpp、step2_2.cpp、step2_3.cpp、step3.cppとmemo.mdとなります。

memo.md内に各ステップで感じたことを追記します。

1からスタートするindexとnodeをmap内に記録して下記の3段階で繋げる。
・leftまでは昇順
・left ~ rightは降順
・rightの次から、最後までは昇順

Choose a reason for hiding this comment

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

昇順かどうかの制約ってありましたっけ?
指定されたインデックスの範囲[left, right]のノードの繋ぎ方を逆にする、という話ではないでしょうか。

Copy link
Owner Author

@Ryotaro25 Ryotaro25 Mar 28, 2025

Choose a reason for hiding this comment

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

@TORUS0818

レビューありがとうございます。自分が初めに行った方法は、listに含まれるnode全てにindexを付与して新たにリストを作るイメージにしました。
leftまではindexの昇順、left〜right間は降順、rightの次から最後の要素まではindexの昇順でならべる。

Choose a reason for hiding this comment

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

ありがとうございます。
昇順というとnodeのvalueをイメージしてしまったのですが、indexならそうですね。

return nullptr;
}

int node_count = 1;

Choose a reason for hiding this comment

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

node_countをindex_to_nodeという名前のmapに入れて検索していると思うので、この変数はnode_indexみたいな名前の方が対応があるのかな、と思いました

}

int node_count = 1;
map<int, ListNode*> index_to_node;

Choose a reason for hiding this comment

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

indexは1から順に並ぶので、mapじゃなくて配列でも同様のことができると思いました


int node_count = 1;
map<int, ListNode*> index_to_node;
auto node = head;
Copy link

@irohafternoon irohafternoon Mar 28, 2025

Choose a reason for hiding this comment

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

auto型は、どのような時に使うのか教えていただきたいです!
(私はC++初心者で、とりあえず型宣言できるものは全てしとこう、くらいの温度感なので)

ListNode dummy_head;
node = &dummy_head;

int current_index = 1;

Choose a reason for hiding this comment

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

current_index の意味は「今見ているノードの次のindex」という感じですかね?(nodeがdummyからスタートしているので)その意味だとcurrentとついているのは少し違和感がありました

@@ -0,0 +1,35 @@
## ステップ1
一旦全てのnodeを分解して、つなぎ合わせる。

Choose a reason for hiding this comment

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

ノードの繋ぎ直す順番を管理して、その順番通りに繋ぎ直すという手法が分かりやすくて参考になりました!

mapやstackを使わないので改善されているか

一つのループ内でnodeが3種類出てくるので処理を追うのが少し大変
近年のPCスペックだと空間計算量に対してそこまでシビアになる必要がないと聞くこともあるので
Copy link

Choose a reason for hiding this comment

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

これはそうだと思います。
一応、どれくらいのメモリーが必要か見積もってみましょう。

(出題意図に繋ぎ変えのお手玉ができるかはありそうですが。)

・left ~ rightは降順
・rightの次から、最後までは昇順
時間計算量O(n)
空間計算量O(n)
Copy link

Choose a reason for hiding this comment

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

追加の空間計算量 O(1) で実装することはできますか?

ListNode* node = head;
while (node) {
index_to_node[num_nodes] = node;
num_nodes++;
Copy link

Choose a reason for hiding this comment

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

num_nodesだとnode全体の数という感じがしました。
node_countのほうが好みです


return dummy_head.next;
}
};
Copy link

Choose a reason for hiding this comment

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

なぜlistではだめなのだろうかと思いましたが、読みやすいです。

reversed->next = next_node->next;
// 初回はnode->nextはleftの先頭にいる
next_node->next = node->next;
node->next = next_node;
Copy link

@fuga-98 fuga-98 Apr 2, 2025

Choose a reason for hiding this comment

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

nodeの役割が変わるのが気になりました。
前のforのように動いてほしいなと感じました。

(繋ぎ方が複数あるみたいなのでイメージと違っていたらすみません)
left -1 のnodeをfront_reversedなどとして、
nodeをfront_reversed
next_nodeをnodeにするのはどうでしょうか。

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.

7 participants