Skip to content

Create LinkedListCycleII.md#2

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

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

Conversation

@kt-from-j
Copy link
Owner

今回解いた問題:
142. Linked List Cycle II
次に解く問題:
83. Remove Duplicates from Sorted List
言語: Java

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.

コメントや関数の切り分けなどが丁寧にされていて読みやすかったです!

Setの解法についてはあまり変更する余地が思いつかなかったので、フロイドの循環検出法で実装
## 解答
- ループ検知部分を関数化してみる。
- collisionがパッと思い浮かんだのですがcollisionは違和感あるでしょうか?
Copy link

Choose a reason for hiding this comment

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

(GPTに聞いた意見ですが)collisionは例外やバグっぽい響きがあるということでGPTにはmeetingを勧められました

Copy link
Owner Author

Choose a reason for hiding this comment

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

やはりcollisionはそういうニュアンスを感じますよね。
「出会う」や「追いつく」みたいなニュアンスの語をあてた方が親切でした。ありがとうございます!

```java
public class Solution {
public ListNode detectCycle(ListNode head) {
if (head == null) {
Copy link

Choose a reason for hiding this comment

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

一応こちらの条件分岐は無くてもいけますね。nullの場合は確かに例外的な感じがするのであってもよいとも思います。

ListNode fromStart = head;
ListNode fromCollision = collisionNode;

while (fromStart == fromCollision) {
Copy link

Choose a reason for hiding this comment

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

javaに詳しくないので違うかもしれませんが、等しくない限り続けるので!=ではないでしょうか?

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.

これは、step2通ったんでしょうか?
通してみることをおすすめします。通るのであれば原因が気になりますし。通してないのであれば、それはそれで手に馴染んでないので気になります。

Choose a reason for hiding this comment

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

あー理解しました。Step3で通す前に修正して、passは考えてない感じなんですね。すいません

Copy link
Owner Author

Choose a reason for hiding this comment

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

ご指摘のとおり、正しく動作しません。
コード整理するときにミスってました。
きちんと確認する癖つけたいと思います。


## 解答2
- [Rでフロイドの循環検出法を可視化する](https://qiita.com/nozma/items/bfa3e089cd432b74c10d) で始点検出方法を理解し、実装してみる
- 運用するプログラムの場合、フロイドの循環検出法の概要からコメントを書くが、この場合どこまで書くか
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.

たしかにそれくらいは書いておくべきでした。
名前さえ分かればググれますからね。

@Kota-Isayama
Copy link

stepを重ねるごとにコードが読みやすくなっており、素晴らしいなと思いました!

@brood0783
Copy link

Set解法優先、命名も良い感じで文句ないです。全体的に読みやすいです👍

return null;
}

HashSet<ListNode> visitedNodes = new HashSet<>();
Copy link

Choose a reason for hiding this comment

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

自分なら、HashSet の実装に依存しないコードであれば、

Set<ListNode> visitedNodes = new HashSet<>();

のように、インターフェースで受け取るのですが、好みの問題かもしれません。

Copy link
Owner Author

Choose a reason for hiding this comment

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

確かにHashSet固有のメソッドを使用していないので、HashSetで宣言する意味はあまり無いですね。
一旦、nodchipさんの好みを真似してインターフェース優先で宣言するようにしてみます。



# step2 他の方の解答を見る
Setの解法についてはあまり変更する余地が思いつかなかったので、フロイドの循環検出法で実装

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.

実装例の紹介ありがとうございます!

```


# step2 他の方の解答を見る

Choose a reason for hiding this comment

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

どの方の解答を参考したのかがわかるようにPRのリンクを貼っておくと、参考にした実装からどのあたりを変えた・変えなかったのかがわかってより思考プロセスが浮き彫りになり、議論が盛り上がるかなと思いますので、貼ることをお勧めします。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます!
次のPRあたりから参照したPRを明示し始めました。

```

# step3 3回ミスなく書く
Set解法で。

Choose a reason for hiding this comment

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

ちなみに Set 解法で step 3に臨もうと思った理由はなんでしょうか?
step 2で hare and tortoise の解法を学んでいらっしゃったのであえてまた Set に戻して解いた理由が気になります。

Copy link
Owner Author

Choose a reason for hiding this comment

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

Setを用いた実装のほうがシンプルで分かりやすいからです。
hare and tortoiseの方が高速なのは確かですが、そこの高速化が必要になる場面は少ないかなと思いまして。
それであれば誰が見ても一目で分かるSetを使った解法を優先すべきかなと判断しました。

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