Single-chain surface test-chain strap ring problem

Source: Internet
Author: User

1, determine whether a single-linked list with a ring

Thinking Analysis:

To determine whether a single-linked list is a band, it is possible to traverse through a single-linked list, and if the loop is stuck, it will never end, but this method of judgment is impossible to implement in the program. So switch to a way of thinking, using two different traversal speed of the pointer traversal, if there is a ring, then the fast pointer will catch up with the slow pointer sooner or later. It is relatively simple and feasible to realize this procedure through this judgment.

The definition of a single linked list structure and its class

struct Node{public:node (const datatype& D): _data (d), _next (NULL) {}public:datatype _data; Node* _next;}; Class List{public:list (): _head (null), _tail (null) {}~list () {node* cur = _head;while (cur && cur!=_tail) {node* del = Cur;cur = Cur->_next;delete del;} Delete _tail;//because the next field of the tail pointer is not empty if there is a ring, which causes the _head = null;//Ring entry point to be refactored two times, causing the program to crash _tail = NULL;} private:node* _head; Node* _tail;};

Judging whether a single-linked list is not an existing ring

node* list::checkcycle () {node* slow = _head;//slow pointer node* fast = _head;//fast pointer while (fast && fast->_next) {slow = SL Ow->_next;fast = fast->_next->_next;if (fast = = slow) {return slow;//loop returns the node that met}}return null;//no ring}

2, to find the length of the ring

Thinking Analysis:

In the above has found a fast and fast pointer to meet the node, we can through the node at the meeting to re-traverse, and introduce a counter, until the meeting node again reached.

int list::getcirclelength (node* meet) {node* start = meet; node* end = Meet;int Count = 0;//counter Do{start = start->_next;count++;} while (Start! = end); return count;}

3. Find the entry point of the ring

1) A pointer is traversed from the node that meets, and the other one traverses from the beginning until two pointers meet, which is the entry point.

node* List::getcycleentrynode (node* meetnode) {node* start = _head; node* end = Meetnode;while (start! = end) {start = Start->_next;end = End->_next;} return start;}

2) The loop is split into two linked lists in the next node of the fast and fast pointer encounters, so that the problem can be transformed into the intersection of the linked list, and the intersection of the linked list is the ring entry point.

node* List::getcycleentrynode (node* meetnode) {Node *l1 = _head; Node *L2 = meetnode->_next;//starts at the next node of the meeting node Meetnode->_next = null;//assigns the _next field of the tail node of the two single-linked list to null node *enter = Getcrossnode (L1, L2);//Split the single-linked list into two strips and find the intersection Meetnode->_next = l2;//Restores the original single-linked list to return Enter;}

Single-chain surface test-chain strap ring problem

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.