Floyd’s Tortoise and Hare Problem
141. Linked List Cycle
Leetcode: https://leetcode.com/problems/linked-list-cycle/description/
Solution 1:
Use fast & Slow two pointers, if a linkedlist has circle, the fast will finally meet with the slow pointer in the cycle
1 | public boolean hasCycle(ListNode head) { |
1 | def hasCycle(self, head): |
Solution 2:
Recursive solution, mark all the visited node (pointing to itself), if meet a node that pointing to itself, then there is a cycle
1 | public boolean hasCycle(ListNode head) { |
142. Linked List Cycle II
Leetcode: https://leetcode.com/problems/linked-list-cycle-ii/description/
Video: https://www.youtube.com/watch?v=-YiQZi3mLq0
[Solution]
1 | def detectCycle(self, head): |
287. Find the Duplicate Number
Leetcode: https://leetcode.com/problems/find-the-duplicate-number/description/
Solution: https://leetcode.com/problems/find-the-duplicate-number/solution/
[Solution] Similar to cycle in the linked list. See details above.
- We are sure that there’s duplicate in the array: there’s a cycle
- Difference with the cycle list: next node
i+1
is represented asnums[nums[i]]
. Thus, slow runner isnums[nums[i]]
, fast runner isnums[nums[nums[i]]]
- Find the intersection of two runners first
- Find the entrance of the cycle, the entrance is the duplicate number
1 | class Solution { |
Find Point Problem
109. Convert Sorted List to Binary Search Tree
Leetcode: https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/description/
[Solution]
- Use fast and slow pointers to find the middle point of the linked list
1 | fast = fast.next.next |
- The fast pointer actually become useless after finding the middle point
After we find the middle node (after fast reach to the end), we split the linked list to left sublist (that’s why I used a prevSlow pointer, since we need to cut the list off at the middle pointer in order to use the left sublist!) and right sublist, which would also be the left subtree and right subtree.
1
2
3
4
5
6
7[-10,-3,0,5,9]
^ ^
slow fast
middle
left: [-10,-3]
right: [5,9]Then we repeat the 1,2,3 steps until we finish the whole list
1 | class Solution { |
109. Convert Sorted List to Binary Search Tree
Leetcode: https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/description/
[Solution]
- Use fast and slow pointers to find the middle point of the linked list
1 | fast = fast.next.next |
- The fast pointer actually become useless after finding the middle point
After we find the middle node (after fast reach to the end), we split the linked list to left sublist (that’s why I used a prevSlow pointer, since we need to cut the list off at the middle pointer in order to use the left sublist!) and right sublist, which would also be the left subtree and right subtree.
1
2
3
4
5
6
7[-10,-3,0,5,9]
^ ^
slow fast
middle
left: [-10,-3]
right: [5,9]Then we repeat the 1,2,3 steps until we finish the whole list
1 | class Solution { |
61. Rotate List
Leetcode: https://leetcode.com/problems/rotate-list/
Note:
- Use two pointers
l
, andr
with a distance ofk
- Beware of the cases where
k > len(list)
andk == len(list)
- If we don’t want to count the length of the whole list, we could set
r
tohead
everytime it hits the end. But that would lead to TLE whenk
is very large.
1 | def rotateRight(self, head, k): |
Odd & Even Pointers
328. Odd Even Linked List
Leetcode: https://leetcode.com/problems/odd-even-linked-list/
Note: Use two pointers odd
and even
to track odd and even nodes.
1 | def oddEvenList(self, head: ListNode) -> ListNode: |