[Top 101 for Niuke interviews] Day 2. Determine whether there is a ring in the linked list and the last k nodes in the linked list.

About the author: Hello everyone, my name is Weiyang;

Blog homepage: Weiyang.303

Column series: TOP101 must-dos for Niuke interviews

Quote of the day: There is only one chance in a person's life to make a difference, and that is now! ! ! ! !

Article directory

Article directory

Preface

1. Determine whether there is a cycle in the linked list

Question description

 Problem solving analysis

2. The last k nodes in the linked list

Question description

 Problem solving analysis

Summarize



Preface

Today is the second day of algorithm questions. In today’s questions, we mainly learned to master the related question types of double pointers (fast and slow pointers);


1. Determine whether there is a cycle in the linked list

Question description

describe

Determine whether there is a cycle in the given linked list. Returns true if there is a loop, false otherwise.

Data range: linked list length 0≤100000≤n≤10000, the value of any node in the linked list satisfies ∣val∣<=100000;

Requirements: Space complexity O(1), time complexity O(n).


The input is divided into two parts, the first part is the linked list, and the second part represents whether there is a cycle, and then the composed head node is passed into the function. -1 represents no loop, and other numbers represent loops. The explanation of these parameters is only to facilitate readers' self-testing and debugging. What is actually read during programming is the head node of the linked list.


for example:

For example, when you enter {3,2,0,-4},1, the corresponding linked list structure is as shown below:

It can be seen that the entry node of the ring is the first node starting from the head node (note: the head node is the 0th node), so the output is true. 


Example 1:


Example 2:


Example 3:


 Problem solving analysis

Problem-solving ideas:

In this question, we use the double pointer method to analyze the question;

We use two pointers, fast and slow.

They all start at the head of the linked list. Subsequently, the slow pointer moves backward one position at a time, and the fast pointer moves backward two positions.

If there is a loop in the linked list, the fast pointer will eventually meet the slow pointer in the loop again; otherwise, there is no loop; otherwise, there is no loop;


Graphical analysis:


Code writing idea steps:

Step 1. Initialize two pointers, one called "fast pointer" (fast) and one called "slow pointer" (slow), and point them both to the head node of the linked list.


Step 2. In the loop, the fast pointer moves two steps at a time and the slow pointer moves one step at a time. This way, the fast pointer will go further than the slow pointer.


Step 3. If there is a cycle in the linked list, the fast pointer will eventually catch up or meet the slow pointer. If there is no loop, the fast pointer will reach the end of the linked list first, completing the loop.


4. When the fast pointer and the slow pointer meet, it means that there is a loop in the linked list, and true can be returned. If the fast pointer reaches the end of the linked list without meeting the slow pointer, it means there is no cycle in the linked list and false can be returned.

Code writing:

2. The last k nodes in the linked list

Question description

describe

Input a linked list of length n, assume the value of the element in the linked list is ai, and return the k-th node from the last in the linked list.

If the length of the linked list is less than k, return a linked list of length 0.


Data range: 0≤1050≤n≤105, 0≤090≤ai​≤109, 0≤090≤k≤109;

Requirements: Space complexity O(n), time complexity O(n).

Advanced: Space complexity O(1), time complexity O(n).


Example:
For example, when you enter {1,2,3,4,5},2, the corresponding linked list structure is as shown below:

 The blue part is the last two nodes of the linked list, so just return the second to last node (that is, the node with a node value of 4), and the system will print all subsequent nodes for comparison.

Example 1:


Example 2:


 Problem solving analysis

Problem-solving ideas:

We use the method of fast and slow pointers to solve this problem;
the first pointer moves k steps first, and then the second pointer starts from the beginning. At this time, the two pointers move at the same time. When the first pointer reaches the end of the linked list, return The second pointer is enough; at this time, the position of the second pointer is the k-th node returned from the bottom;


Graphical analysis:


Code writing idea steps:

1. Initialize two pointers, one called "fast pointer" (fast) and one called "slow pointer" (slow), and point them both to the head node of the linked list.


2. Move the fast pointer forward k nodes first, so that the difference between the fast pointer and the slow pointer is k nodes.


3. Then, move the fast and slow pointers simultaneously until the fast pointer reaches the end of the linked list. In this process, the distance between the fast and slow pointers is always maintained by k nodes.


4. When the fast pointer reaches the end of the linked list, the node pointed by the slow pointer is the k-th node from the bottom.


Note: If the length of the linked list is less than k, that is, the linked list has ended before the fast pointer reaches the end, then the k-th node from the bottom cannot be found.


Code writing:

Summarize

Guess you like

Origin blog.csdn.net/qq_64861334/article/details/131981621