[Single Linked List Exercise] - Determine whether there is a ring in the linked list

topic description

Description
Given a linked list, determine whether there is a cycle in the linked list.
insert image description here


Problem solving analysis:

Idea:
Two pointers point to the same head node, one pointer is slow, one step at a time, and one pointer is fast, two steps at a time.
The fast pointer enters the ring first, and when the fast pointer completes N laps in the ring, the slow pointer just enters the ring, and the fast pointer starts to catch up with the slow pointer that entered the ring.
When the address of the fast pointer is equal to the address of the slow pointer, they meet, indicating that the linked list is a circular linked list.
If there is no ring, fast will go to empty. If there is a ring, fast will catch up with slow in the ring.

Code:

struct ListNode
{
    
    
	int val;
	struct ListNode* next;
};
int hasCycle(struct ListNode* head)
{
    
    
	struct ListNode* slow = head;
	struct ListNode* fast = head;
	while (fast != NULL && fast->next != NULL)//循环条件:可能不为环,此时奇数个结点fast为空结束,偶数个结点fast->next为空结束
	{
    
    
		slow = slow->next;
		fast = fast->next->next;
		if (slow == fast)
		{
    
    
			return 1;
		}
	}
	return 0;
}

Thinking:
Interviewer: Please prove how the fast pointer catches up with the slow pointer?
What if the slow pointer moves 1 step at a time, and the fast pointer moves 3 steps at a time? 4 steps? n steps?
Note: After entering the ring, fast follows the slow pointer.
1. The first case: the slow pointer takes 1 step, and the fast pointer takes 2 steps (in this case, the slow pointer can only go a circle at most).
Assume that after the slow pointer enters the ring, the distance between fast and slow is X
when they are at the same time Every time they walk, the distance between them decreases by 1, that is, X+1-2=X-1.
When they keep walking, X-1, X-2, X-3... When X = 0, the two will definitely meet.
2. The second case: the slow pointer takes one step, and the fast pointer takes 3 steps.
Assume that after the slow pointer enters the ring, the distance between fast and slow is X (in this case, the slow pointer may walk multiple times, and may meet)
when they are at the same time Every time they walk, the distance between them is reduced by 2, that is, X+1-3=X-2.
When they keep walking, X-2, X-4, X-6...
At this time, if x is an even number (a multiple of 2 ) will meet. If X is an odd number (not a multiple of 2), when X = -1, the two are staggered.
The next catch-up, the difference between the two is C-1, where C is the length of the ring. If C-1 is an odd number, it will never catch up afterwards. If it is an even number, you can catch up.
That is, when N=C-1, each time the gap is reduced by 2, if the gap is odd in extreme cases (infinite loop), it will always be missed, resulting in an infinite loop.
3. The third case: the slow pointer takes one step, and the fast pointer takes 4 steps.
Assume that after the slow pointer enters the ring, the distance between fast and slow is X (in this case, the slow pointer may walk a circle at most, and may meet)
when they At the same time, every time they walk, the distance between them is reduced by 3, that is, X+1-4=X-3
When they keep walking, X-3, X-6, X-9...
At this time, if x is (a multiple of 3), they will meet. If X is (not a multiple of 3), when X = -2, the two are staggered.
In the next catch-up, the difference between the two is C-1 or C-2, where C is the length of the ring. If C-1 or C-2 is not a multiple of 3, it will never catch up afterwards. If it is a multiple of 3, you can meet.
That is, when N=C-1 or C-2, each time the gap is reduced by 2, if the gap is not a multiple of 3 in extreme cases (infinite loop), it will always be missed, resulting in an infinite loop.
Therefore: when the slow pointer moves one step at a time, the fast pointer moves N steps at a time, similar reasoning.
So how to find the entry point? Returns a pointer to the entry point. For details, please refer to this article " [Single Linked List Exercise] - Seeking the Entry Point of a Circular Linked List ".


Guess you like

Origin blog.csdn.net/qq_48163964/article/details/130011945