First, the fast and slow pointer method
1. defines two pointers, a fast pointer and a slow pointer, both of which initially point to the head node of the linked list.
2. The fast pointer moves two nodes at a time, and the slow pointer moves one node at a time.
3. If there is a ring in the linked list, the fast pointer and the slow pointer will meet at a certain node. If the fast pointer and the slow pointer meet at the end of the linked list, there is a ring in the linked list.
4. If the fast pointer and the slow pointer meet at the head of the linked list, there is no ring in the linked list.
Second, the hash table method
1. Define a hash table (or use a collection) to store the nodes that have been traversed.
2. Traverse the linked list, and for each node, check whether it already exists in the hash table. If it exists, there is a ring in the linked list. If it does not exist, the node will be added to the hash table.
3. If no duplicate nodes are found in the whole linked list, it means that there are no rings in the linked list.
Third, the mathematical formula method
1. Define two variables, one is a fast variable and the other is a slow variable, both of which are 1 initially.
2. Traverse the linked list. For each node, multiply the fast variable and the slow variable by the value of the current node, and then add the result to the fast variable and the slow variable.
3. If there is a ring in the linked list, the fast variable and the slow variable will meet at a certain node. If fast variables and slow variables meet at 1, there is a ring in the linked list.
4. If fast variables and slow variables never meet, then there are no rings in the linked list.
Features of linked list:
1, single linked list: the first node and the last node are the head node and the tail node respectively. The head node is used to record the base address of the linked list. With it, we can traverse the whole linked list. The tail node is special in that the pointer does not point to the next node, but to an empty address NULL, indicating that this is the last node on the linked list.
2. Two-way linked list: One-way linked list has only one direction, and the node has only one subsequent pointer next to point to the next node. Linked list, as its name implies, supports two directions. Each node not only has a successor pointer next to the back node, but also has a predecessor pointer prev to the front node. A two-way linked list needs two extra spaces to store the addresses of successor nodes and predecessor nodes.
3. Circular linked list: Circular linked list is a special single linked list. In fact, the only difference between circular linked list and single linked list is the tail node. The tail node pointer of a single linked list points to an empty address, indicating that this is the last node, while the tail node pointer of a circular linked list points to the head node of the linked list, forming a linked list connected end to end like a ring, so the circular linked list.