Skip to content

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

206. Reverse Linked List #22

wants to merge 6 commits into from

Conversation

katsukii
Copy link
Owner

問題

https://leetcode.com/problems/reverse-linked-list/

言語

Java

次に解く問題

Kth Largest Element in a Stream

Comment on lines +190 to +192
if (head == null || head.next == null) {
return head;
}
Copy link

Choose a reason for hiding this comment

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

この条件はなくてもいいでしょうか。(あってもいいですが。)

Copy link
Owner Author

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
- 再帰で解く方法があるようなのでチャレンジしたい
Copy link

Choose a reason for hiding this comment

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

再帰は見ておいてもいいかもしれません。

Copy link
Owner Author

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 とつけた。この命名がレビュアーにどう思われるかは気になるところ

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 などでも違和感ないかもしれないです。

Choose a reason for hiding this comment

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

oldNextを使うのであればprevをnewNextとしたほうが読みやすいかもしれないです。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。いずれも参考にさせていただきます。

Copy link

@hayashi-ay hayashi-ay left a comment

Choose a reason for hiding this comment

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

ざっと気になったところにコメントしました

空間計算量: O(n)

- 最初に思いついたやり方
- 各ノードを配列に一度保存して逆順に再度リンクを張っていく

Choose a reason for hiding this comment

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

Stackに入れるのでも良いかも。そちらの方が処理はシンプルになりそう。

Copy link
Owner Author

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;

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;

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(今回は単方向なのでないですが)があって、結構似たような変数が増えると結構読むのが難しいなという感覚があります

Copy link
Owner Author

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;
Copy link

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); としてもよいと思いました。

Copy link
Owner Author

Choose a reason for hiding this comment

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

たしかによく考えたらわざわざ宣言しなくても良いですね

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.

6 participants