Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fb3bd9b

Browse files
LC#142 added brute force approach- find first meet point in linkedlist cycle
1 parent c804312 commit fb3bd9b

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

‎Leetcode/leetcodeTags/LinkedList/LinkedListCycleII142.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package LinkedList;
22

3+
import java.util.HashMap;
4+
import java.util.Map;
5+
36
public class LinkedListCycleII142 {
47

58
class ListNode {
@@ -12,6 +15,22 @@ class ListNode {
1215
}
1316
}
1417

18+
// brute force way
19+
public ListNode detectCycleII(ListNode head) {
20+
if (head == null || head.next == null)
21+
return null;
22+
23+
Map<ListNode, Boolean> map = new HashMap<>();
24+
25+
ListNode start = head;
26+
while (start != null && !map.containsKey(start)) {
27+
map.put(start, true);
28+
start = start.next;
29+
}
30+
31+
return start;
32+
}
33+
1534
public ListNode detectCycle(ListNode head) {
1635

1736
if (head == null || head.next == null)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /