Skip to content

141. Linked List Cycle#3

Open
rossy0213 wants to merge 1 commit intomainfrom
arai60-1
Open

141. Linked List Cycle#3
rossy0213 wants to merge 1 commit intomainfrom
arai60-1

Conversation

@rossy0213
Copy link
Copy Markdown
Owner

while (head != null) {
if (foundNode.contains(head)) {
return true;
} else {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この else 節はまるっと下げるほうが私は好みです。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

else自体自分もあまり好まないので、気持ちわかります。

ただ、これを下げるというのはどうすべきでしょうか?
こんな形でしょうか?

boolean found = foundNode.contains(head);
if (found) {
    //...
}

if (!found) {
     // ...
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

if 節が return なので、
if (A) {
return X;
} else {
B;
}
C;

if (A) {
return X;
}
B;
C;
と同じですよね、ということです。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

あ、ごめんなさい。脳内試行でfast returnできない勘違いしてました。

ListNode oneStepNode = head;
ListNode twoStepNode = head;

while ((twoStepNode != null) && (twoStepNode.next != null)) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Java の Operator Precedence 的に、() が不要ですか?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

不要ですね。これはIDEに貼り付けた後に、つけた方がわかりやすいぞって一度適用して形確認したものです。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

本質ではないのですが、 IDE を使わずに練習したほうが良いかもしれません。面接本番では IDE 使えないと思いますので。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

IDE使ってないです。このmarkdownを書くときにleetcodeのエディターからコピーしており、そのときにヒントで出てたので、適用させてみたっていうところです。

}
}

return false;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

while (1) {
if (A) {return false;}
....
if (B) {return true;}
}
という構造ですね。
do while を使うなど色々な選択肢があることは頭においておきましょう。この構造を別のものと組み合わせるときに使うことになります。

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.

3 participants