Skip to content

Commit

Permalink
problem: 0142 linked list cycle II
Browse files Browse the repository at this point in the history
  • Loading branch information
cychiu8 committed Jan 26, 2022
1 parent f00aaaf commit b873d72
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions 0142 Linked List Cycle II/0142 Linked List Cycle II.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# 0142. Linked List Cycle II

* Difficulty: medium
* Link: https://leetcode.com/problems/linked-list-cycle-ii/
* Topics: Linked-List, Multiple-Pointers

# Clarification

1. Check the inputs and outputs
- INPUT: Linked List
- OUTPUT: ListNode
2. Check the main goal
- find entry point

# Naive Solution

### Thought Process

1. slow, fast pointer to find the meet point
2. two pointer start from meet point and beginning
3. the meet point is the entry point
- Implement

```python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None

class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
# find cycle
slow = head
fast = head
while slow and fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
break

if slow == None or fast == None or fast.next == None:
return None

# find meet point
fast = head
while slow != fast:
slow = slow.next
fast = fast.next

return slow
```


### Complexity

- Time complexity: $O(n)$
- Space complexity:$O(1)$

# Note

- Related

[0**287. Find the Duplicate Number** ](https://www.notion.so/0287-Find-the-Duplicate-Number-1f3d3e7e0ab74f22b4402128fc8351ba)

0 comments on commit b873d72

Please sign in to comment.