LeetCode - speed pointer

Pointer speed: a dual pointer. Two pointers provided in the list, the list does not traverse the synchronization. We can manufacture the required distance between the two pointers.

 

LeetCode 141 circular linked list     easy

To test surface title purely by changes in the sample, as long as the list is determined by whether there is a loop made on the line.

A thought: Hash

Most likely to think the idea should be hashing, whenever a list to be visited, and put it recorded in the hash table, when there will be a node in the ring list of repeat visits, the code slightly.

Thinking two: the speed of the pointer

Providing two pointers, fast and slow, step 2, respectively. If the list compared to the runway, which means fast twice as fast as the slow. When the ring is present in the list, then quickly fast access to not NULL, but fast speed as fast, slow sooner or later than one full turn, i.e., after some time two pointers will again meet (fast == slow).

 1 class Solution {
 2 public:
 3     bool hasCycle(ListNode *head) {
 4         if(NULL==head||NULL==head->next)
 5             return false;
 6         
 7         ListNode *slow=head;
 8         ListNode *fast=head;
 9         
10         while(NULL!=fast&&NULL!=fast->next)
11         {
12             fast=fast->next->next;
13             slow=slow->next;
14             if(fast==slow)
15                 return true;
16         }
17         return false;
18     }
19 };
View Code

 

The intermediate node list LeetCode 876  easy

Fast speed of the pointer is twice slower pointer, when the pointer reaches the end of the fast, slow pointer points exactly intermediate node list.

 1 class Solution {
 2 public:
 3     ListNode* middleNode(ListNode* head) {
 4         ListNode *slow=head;
 5         ListNode *fast=head;
 6         if(NULL==head||NULL==head->next)
 7             return head;
 8         while(1)
 9         {
10             fast=fast->next;
11             if(NULL==fast->next)
12                 return slow->next;
13             slow=slow->next;
14             fast=fast->next;
15             if(fast->next==NULL)
16                 return slow;
17         }
18     }
19 };
View Code

 

Reciprocal LeetCode 19 to delete the list of N nodes     medium

N steps so that fast pointer first, and then traverse the list at the same speed, when the pointer reaches the end of the fast, slow right pointer points to the N-th element before the fast pointer.

 1 class Solution {
 2 public:
 3     ListNode* removeNthFromEnd(ListNode* head, int n) {
 4         ListNode *slow=head;
 5         ListNode *fast=head;
 6         for(int i=0;i!=n;++i)
 7             fast=fast->next;
 8         if(NULL==fast)
 9             return head->next;
10         while(NULL!=fast->next)
11         {
12             fast=fast->next;
13             slow=slow->next;
14         }
15         slow->next=slow->next->next;
16         return head;
17     }
18 };
View Code

Guess you like

Origin www.cnblogs.com/CofJus/p/11992789.html