-
Notifications
You must be signed in to change notification settings - Fork 0
141. Linked List Cycle #3
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 { | ||
| foundNode.add(head); | ||
| head = head.next; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
| ``` | ||
| 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)) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Java の Operator Precedence 的に、() が不要ですか?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 不要ですね。これはIDEに貼り付けた後に、つけた方がわかりやすいぞって一度適用して形確認したものです。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 本質ではないのですが、 IDE を使わずに練習したほうが良いかもしれません。面接本番では IDE 使えないと思いますので。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. while (1) { |
||
| } | ||
| } | ||
| ``` | ||
|
|
||
| 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 | ||
| ``` | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この else 節はまるっと下げるほうが私は好みです。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
else自体自分もあまり好まないので、気持ちわかります。ただ、これを下げるというのはどうすべきでしょうか?
こんな形でしょうか?
There was a problem hiding this comment.
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;
と同じですよね、ということです。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あ、ごめんなさい。脳内試行でfast returnできない勘違いしてました。