-
Notifications
You must be signed in to change notification settings - Fork 0
206. Reverse Linked List #22
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
if (head == null || head.next == null) { | ||
return 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.
この条件はなくてもいいでしょうか。(あってもいいですが。)
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.
たしかになくてもいいですね。
他の方が描いたコードを見て、参考にしてコードを書き直してみる。 | ||
参考にしたコードのリンクは貼っておく。 | ||
読みやすいことを意識する。 | ||
他の解法も考えみる。 | ||
|
||
- https://github.com/shintaroyoshida20/leetcode/pull/12/files#diff-62f050819b0cae018db7450bb0f0341942d799ece31ce3d0d1d4a64d45578363R24 | ||
- 再帰で解く方法があるようなのでチャレンジしたい |
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.
再帰は見ておいてもいいかもしれません。
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.
はい、Approach 5の方でトライしてみました。
空間計算量: O(1) | ||
|
||
- リンクを順番にひっくり返していく方法 | ||
- 単に next だと再接続前か後かがわかりづらかったので、old とつけた。この命名がレビュアーにどう思われるかは気になるところ |
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.
oldNext が node.next ということは、新しい接続先(newNext)が prev ということでしょうか。「リンクに注目する」という意味を捉えられているかわかりませんが、自分としては nextNode などでも違和感ないかもしれないです。
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.
oldNextを使うのであればprevをnewNextとしたほうが読みやすいかもしれないです。
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.
ありがとうございます。いずれも参考にさせていただきます。
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(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.
Stackに入れるのでも良いかも。そちらの方が処理はシンプルになりそう。
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 = nodes.get(i); | ||
node.next = nodes.get(i - 1); | ||
} | ||
nodes.get(0).next = null; |
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に入れてあげる際(L29のwhile loop内)にnext
についてとりあえずnullを入れていても良いかもしれないです。
return head; | ||
} | ||
|
||
ListNode prev = null; |
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.
lastSeen
とかはどうでしょうか。Linked List自体にもnext, prev(今回は単方向なのでないですが)があって、結構似たような変数が増えると結構読むのが難しいなという感覚があります
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.
lastSeen いいですね。
似たような変数が増えると結構読むのが難しい
この観点はなかったのでたしかにと思いました。ありがとうございます。
node.next = nodes.get(i - 1); | ||
} | ||
nodes.get(0).next = null; | ||
return reverseHead; |
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.
reverseHead を宣言せず、 return nodes.get(nodes.size() - 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.
たしかによく考えたらわざわざ宣言しなくても良いですね
問題
https://leetcode.com/problems/reverse-linked-list/
言語
Java
次に解く問題
Kth Largest Element in a Stream