Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions LinkedListCycle/step1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
```java
// Create a hashSet to store all found node, if we find the same node again meaning there is a cycle then return true.
// Time complexity: O(N) N: number of node
// Space complexity: O(N)
// Time spent: 04:50
public class Solution {
public boolean hasCycle(ListNode head) {
Set<ListNode> foundNode = new HashSet<>();

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できない勘違いしてました。

foundNode.add(head);
head = head.next;
}
}

return false;
}
}
```
69 changes: 69 additions & 0 deletions LinkedListCycle/step2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
```java
public class Solution {
public boolean hasCycle(ListNode head) {
Set<ListNode> visitedNode = new HashSet<>();

while (head != null) {
if (visitedNode.contains(head)) {
return true;
}
visitedNode.add(head);
head = head.next;
}

return false;
}
}
```

```java
// After checking solutions, found a faster O(1) space solution using two pointers
// Create 2 pointers, 1 pointer is trying to visit next node, and other pointer is trying to visit next next node
// If there is a cycle, the two-step pointer will eventually catch up with the slow pointer
// Node1 -> Node2 -> Node3 -> Node4 -> Node5 -> Node2
// Index 0 1 2 3 4 5
// 5 6 7 8 9
// Index of one-step pointer: 0(n1), 1(n2), 2(n3), 3(n4), 4(n5)
// Index of two-step pointer: 0(n1), 2(n3), 4(n5), 6(n3), 8(n5)
// Time complexity: O(N) N: number of node
// Space complexity: O(1)
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode oneStepNode = head;
ListNode twoStepNode = head;

while ((oneStepNode != null) && (twoStepNode != null) && (twoStepNode.next != null)) {
oneStepNode = oneStepNode.next;
twoStepNode = twoStepNode.next.next;

if (oneStepNode == twoStepNode) {
return true;
}
}

return false;
}
}
```

```java
// only need to check nullable condition for twoStepNode since it runs faster
public class Solution {
public boolean hasCycle(ListNode head) {
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のエディターからコピーしており、そのときにヒントで出てたので、適用させてみたっていうところです。

oneStepNode = oneStepNode.next;
twoStepNode = twoStepNode.next.next;

if (oneStepNode == twoStepNode) {
return true;
}
}

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 を使うなど色々な選択肢があることは頭においておきましょう。この構造を別のものと組み合わせるときに使うことになります。

}
}
```

6 changes: 6 additions & 0 deletions LinkedListCycle/step3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
```java
// 1st try: time spend: 01:25
// 2nd try: time spend: 01;28
// 3rd try: time spend: 01:12
```