Skip to content

Commit

Permalink
对双指针文章存放路径进行规范
Browse files Browse the repository at this point in the history
  • Loading branch information
labuladong committed Mar 4, 2020
1 parent 00771d1 commit 79baa00
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 5 deletions.
File renamed without changes
File renamed without changes
File renamed without changes
10 changes: 5 additions & 5 deletions double_pointer.md → think_like_computer/double_pointer.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ boolean hasCycle(ListNode head) {
```
**2. Knowing that the linked list contains a ring, return to the starting position of the ring**

![1](../pictures/11.png)
![1](../pictures/double_pointer/11.png)

This problem is not difficult at all, look directly at the code:

Expand All @@ -69,13 +69,13 @@ It can be seen that when the "fast" and "slow" pointers meet, let any one of the

For the first encounter, suppose the slow pointer "slow" moves k steps, then the fast pointer "fast" must move 2k steps, which means that "fast" moves k steps more than "slow" (The length of the ring)

![4](../pictures/环1.png)
![4](../pictures/double_pointer/22.png)

Suppose the distance between the meeting point and the start point of the ring is m, then the distance between the start point of the ring and the head node "head" is k-m.

Coincidentally, if we continue to k-m steps from the meeting point, we also reach the starting point of the loop.

![5](../pictures/环2.png)
![5](../pictures/double_pointer/33.png)

So, as long as we repoint one of the fast and slow pointers to "head", and then the two pointers move at the same speed, we will meet after k-m steps. The place where we meet is the beginning of the ring.

Expand All @@ -93,7 +93,7 @@ return slow;
```
When the length of the linked list is odd, "slow" happens to stop at the midpoint; if the length is even, the final position of "slow" is right to the middle:

![2]../pictures/22.png)
![2](../pictures/double_pointer/44.png)

An important role in finding the midpoint of a linked list is to "merge sort" the linked list.

Expand Down Expand Up @@ -145,7 +145,7 @@ int binarySearch(int[] nums, int target) {

Look directly at a LeetCode topic:

![3](../pictures/33.png)
![3](../pictures/double_pointer/33.png)


As long as the array is ordered, you should think of the two pointer technique. The solution of this problem is similar to binary search. You can adjust the size of "sum" by adjusting "left" and "right":
Expand Down

0 comments on commit 79baa00

Please sign in to comment.