Skip to content

Commit bd9ce92

Browse files
linked list cycle II
1 parent a574273 commit bd9ce92

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package medium;
2+
3+
import classes.ListNode;
4+
5+
public class LinkedListCycleII {
6+
7+
public ListNode detectCycle(ListNode head) {
8+
ListNode slow = head, fast = head;
9+
while (fast != null && fast.next != null) {
10+
slow = slow.next;
11+
fast = fast.next.next;
12+
if ( slow == fast) {
13+
ListNode slow2 = head;
14+
while (slow2 != slow) {
15+
slow = slow.next;
16+
slow2 = slow2.next;
17+
}
18+
return slow;
19+
}
20+
}
21+
return null;
22+
}
23+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
|157|[Read N Characters Given Read4](https://leetcode.com/problems/read-n-characters-given-read4/)|[Solution](../../blob/master/EASY/src/easy/ReadNCharactersGivenRead4.java)| O(n)|O(1) | Easy|
8888
|155|[Min Stack](https://leetcode.com/problems/min-stack/)|[Solution](../../blob/master/EASY/src/easy/MinStack.java)| O(1)|O(n) | Easy| Stack
8989
|151|[Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/)|[Solution](../../blob/master/MEDIUM/src/medium/ReverseWordsinaString.java)| O(n)|O(n) | Medium|
90+
|142|[Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/)|[Solution](../../blob/master/MEDIUM/src/medium/LinkedListCycleII.java)| O(n)|O(1) | Medium| Linked List
9091
|141|[Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/)|[Solution](../../blob/master/EASY/src/easy/LinkedListCycle.java)| O(n)|O(1) | Easy| Linked List
9192
|140|[Word Break II](https://leetcode.com/problems/word-break-ii/)|[Solution](../../blob/master/HARD/src/hard/WordBreakII.java)| ? |O(n^2) | Hard| Backtracking/DFS
9293
|139|[Word Break](https://leetcode.com/problems/word-break/)|[Solution](../../blob/master/MEDIUM/src/medium/WordBreak.java)| O(n^2)|O(n) | Medium| DP

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy