Nine-chapter algorithm surface examination Questions 28 chain list to find the ring

Source: Internet
Author: User

Nine Chapters count judges Net-original website

http://www.jiuzhang.com/problem/28/


Topics

First Order: Give a single-linked list to determine if the single-linked list has a ring, such as 1->2->3->4->2 is a linked list of existing rings. Additional space required to use O (1).

Advanced: Find the entrance of the ring. Additional Space for O (1) is also required.


Answer

First order: With two pointers, starting from the list head, a slow pointer each step, another fast hand two steps each time. Until they meet (with a ring) or the quick pointer goes to null (no ring).

Advanced: After meeting, move a pointer to the list head, two pointers each move one step at a time, until meet again, the meeting point is the ring entrance.


Interviewer Angle

The problem of fast and fast pointers is not common, the problem with this idea in addition to the chain list to find the ring, there is a list to find the midpoint (require a scan). This problem belongs to the problem of examination, also belongs to I know to know, do not know the problem. Let's remember the answer to this question. Specific analysis, the reader can self-calculation on paper.

Nine-chapter algorithm surface examination Questions 28 chain list to find the ring

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.