Given a circular linked list, an algorithm is implemented to return the Start Node of the ring.
Definition:
Cyclic linked list: the pointer of a node in the linked list points to a previous node, resulting in a ring in the linked list.
Example:
Input: A-> B-> C-> D-> E-> C [node C has already appeared before]
Output: node C
You can use a Map <node *, bool> to solve the problem.
The following is a strange solution to the beauty of programming: fast and slow pointer solution.
Code:
Struct snode {int data; snode * Next;}; snode * findcirlestart (const snode * vhead) {If (vhead-> next = NULL) return NULL; snode * fast = vhead; snode * slow = vhead; while (slow & fast-> next) {slow = slow-> next; fast = fast-> next; if (slow = fast) break;} slow = vhead; while (slow! = Fast) {slow = slow-> next; fast = fast-> next;} Return fast ;}
010 given a circular linked list, implement an algorithm to return the Start Node of the ring (keep it up)