010 given a circular linked list, implement an algorithm to return the Start Node of the ring (keep it up)

Source: Internet
Author: User
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.