Skip to content

Create LinkedListCycle.md#1

Open
kt-from-j wants to merge 1 commit intomainfrom
LinkedListCycle
Open

Create LinkedListCycle.md#1
kt-from-j wants to merge 1 commit intomainfrom
LinkedListCycle

Conversation

@kt-from-j
Copy link
Owner

今回解いた問題:
Linked List Cycle
次に解く問題:
Linked List Cycle II

if (head == null) return false;
HashSet<ListNode> visited = new HashSet<>();
ListNode node = head;
while (node != null && node.next != null) {
Copy link

Choose a reason for hiding this comment

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

これ、node.next のチェックっていります? いや、してはだめということはないですが。

Copy link
Owner Author

Choose a reason for hiding this comment

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

フロイドの循環検出法で書いた時のことが頭の片隅にあり、なんとなくで書いてしまっていたものと思います。

いや、してはだめということはないですが。

このチェックがある場合、ラスト1回のループが不要になるため完全に無意味ではないから駄目ではないということでしょうか。
とはいえループの度にしてもしなくてもいいチェックを行うコストの方が高そうなので、この場合は消した方がベターでしょうね。

```java
public class Solution {
public boolean hasCycle(ListNode head) {
if (head == null) return false;
Copy link

Choose a reason for hiding this comment

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

こっちのチェックは消しても動きますが、visited 構築コストが必ずかかるようになるので、つけておいてもいいでしょう。
ぶらさがり if 文は時々事故の原因になります。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.gblcuhn8bpdi

Copy link
Owner Author

Choose a reason for hiding this comment

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

個人的にはこのようなgurad節的なものを書きたくなります。

確かに毎回{}で囲う方が事故は確実に少なくなりますね。
選べる場合はぶら下がりifを避けるようにいったん心掛けてみます。

Copy link

@nanae772 nanae772 left a comment

Choose a reason for hiding this comment

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

お疲れ様です、全体的にシンプルで分かりやすいコードでした。
少しだけコメントいたしました。

問題文の説明を理解するのに時間を要す。
何のために`pos`という概念があるのか。
とりあえず循環を検知するコードを書いて動かすことで、実装のためというよりテストケースの説明のための概念であることを理解する。
この`pos`はpositionの略でしょうか?
Copy link

Choose a reason for hiding this comment

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

そうだと思います(私もこの問題に関係無いposが出てきて少し混乱した記憶があります)

Copy link
Owner Author

Choose a reason for hiding this comment

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

コメントありがとうございます!
ビギナーなのでleetcodeの問題や変数の名付けなどに戸惑ってしまいました。

```java
public class Solution {
public boolean hasCycle(ListNode head) {
if (head == null) return false;
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.

確かに後続の実装と意味的に分かれてるので改行入れた方が親切ですね。


## 解答
- leetcodeの画面で動かすと、フロイド法は0ms、Set使用時は4msとなる。
- 時間計算量は同じO(n)なのにここまで差がつくのが直感に反した。
Copy link

Choose a reason for hiding this comment

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

時間計算量は定数倍を無視した極限の話なので、今回のようなインプットであればあまり参考にならないかもしれません。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。
これくらいの入力であれば、定数倍の影響が大きいということですよね。
見誤らないよう意識するようにします。

Choose a reason for hiding this comment

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

LeetCode の実行時間はブレがあるので、あまり信用しないほうがよさそうです。
手元で実行して計測してみると違いが分かりやすかったです。
kazizi55/coding-challenges#1 (comment)

Copy link
Owner Author

Choose a reason for hiding this comment

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

当初気づかなかったのですが、同じプログラムでもleetcodeの実行時間ブレますね。
ありがとうございます!

public class Solution {
public boolean hasCycle(ListNode head) {
if (head == null) return false;
HashSet<ListNode> visited = new HashSet<>();
Copy link

Choose a reason for hiding this comment

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

長くはなりますが、 visitedNodes とかのほうが丁寧かなと思いました!
(JavaってlowerCameCaseでしたっけ、調べた感じそうでしたが違ったらすみません。)

Copy link
Owner Author

Choose a reason for hiding this comment

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

やっぱりそちらの方が親切ですね。
型からわかることを書くか書かないかよく悩んでしまいます。

Javaのメソッドや変数名はlowerCamelCaseであってます!

Choose a reason for hiding this comment

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

これも好みかもしれませんが、僕も kt-from-j さんと同様に型からわかることは書かないですね。
ちなみに協会内では、型注釈がない Python でも visited や reached などのように書いている方が多い印象です。node が入ってくることは後の処理から見て自明なのではと個人的には判断しています。

Copy link

@kazizi55 kazizi55 left a comment

Choose a reason for hiding this comment

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

全体的にコードがみやすく、思考プロセスも分かりやすかったです!


## 解答
- leetcodeの画面で動かすと、フロイド法は0ms、Set使用時は4msとなる。
- 時間計算量は同じO(n)なのにここまで差がつくのが直感に反した。

Choose a reason for hiding this comment

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

LeetCode の実行時間はブレがあるので、あまり信用しないほうがよさそうです。
手元で実行して計測してみると違いが分かりやすかったです。
kazizi55/coding-challenges#1 (comment)

public class Solution {
public boolean hasCycle(ListNode head) {
if (head == null) return false;
HashSet<ListNode> visited = new HashSet<>();

Choose a reason for hiding this comment

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

これも好みかもしれませんが、僕も kt-from-j さんと同様に型からわかることは書かないですね。
ちなみに協会内では、型注釈がない Python でも visited や reached などのように書いている方が多い印象です。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.

5 participants