-
Notifications
You must be signed in to change notification settings - Fork 0
92. Reverse Linked List II #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| 1からスタートするindexとnodeをmap内に記録して下記の3段階で繋げる。 | ||
| ・leftまでは昇順 | ||
| ・left ~ rightは降順 | ||
| ・rightの次から、最後までは昇順 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
昇順かどうかの制約ってありましたっけ?
指定されたインデックスの範囲[left, right]のノードの繋ぎ方を逆にする、という話ではないでしょうか。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
レビューありがとうございます。自分が初めに行った方法は、listに含まれるnode全てにindexを付与して新たにリストを作るイメージにしました。
leftまではindexの昇順、left〜right間は降順、rightの次から最後の要素まではindexの昇順でならべる。
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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を分解して、つなぎ合わせる。 | |||
There was a problem hiding this comment.
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スペックだと空間計算量に対してそこまでシビアになる必要がないと聞くこともあるので |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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++; |
There was a problem hiding this comment.
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; | ||
| } | ||
| }; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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にするのはどうでしょうか。
問題へのリンク
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内に各ステップで感じたことを追記します。