Linked list operation--linked list problem

Source: Internet
Author: User

Reference:

http://blog.163.com/[email protected]/blog/static/1113522592011828104617420/

Problem:

Determine if there is a ring in a linked list.

Analysis:

We all know that when there is no ring in a list, we use a pointer to traverse from head to tail, and when there are loops in the list, the list rotates in the ring.

When we use only one linked list pointer, the conceivable method is to use an additional data structure to store each traversed node, and when traversing the next node, determine if the next node already exists in the stored node.

The storage structure can be selected as Hashtable, so that the overall time complexity is O (n) and the spatial complexity is O (n).

When we use two pointers, "List of common tricks to operate!" , you can set a fast, slow two pointer.

If there is no ring in the list, the fast pointer will eventually become null, and if there is a ring, both the fast pointer and the slow pointer go into the ring, because the slow pointer is stationary relative to the fast pointer, and the fast pointer has a slow pointer pace of 1, and the final fast pointer must be able to catch the slow pointer.

This method has a time complexity of O (n) and a spatial complexity of O (1)

Answer:

1) Use Hashtable, not in this table.

2) Use fast, slow pointers.

  

BOOLIshascycle (node* head)Const{    if(head = = NULL | | head->next = =NULL)return false; Node* Slow =Head; Node* Fast = Head->Next;  while(Fast! = NULL && Fast->next! =NULL) {Fast= fast->next->Next; Slow= slow->Next; if(Fast = =slow)return true; }    return false;}

Problem:

Find the first entry point of a linked list.

Analysis:

1) using Hashtable to store traversed nodes, the time complexity of getting into a link point is O (n) and the spatial complexity is O (n).

2) When we use the fast and slow pointer, two nodes will meet each other.

  

Assuming that the length of the linear stage is L, the starting point distance of the two-pointer meeting point is T and the length of the ring is s.

We let the slow pointer start moving from the head, one step at a time, and the quick pointer starts at the Head->next and moves forward two steps each way.

When two pointers meet, the fast pointer rotates the M-circle, and the slow pointer rotates the n-circle.

(l + MXS + T-1)/2 = (L + nxs + T) "Same time"

= = (M-2XN) xs = T + L + 1

= = (m-2xn-1) xs + s-t = L + 1

Obviously, after the encounter, let a pointer start at the beginning of the list, and the other pointer moves from the next node of the meeting point. This way, two pointers meet at the start node of the list.

node* first_node_in_cycle (node* head)Const{    if(head = = NULL | | head->next = =NULL)return false; Node* Slow =Head; Node* Fast = Head->Next;  while(Fast! = NULL && Fast->next! =NULL) {Fast= fast->next->Next; Slow= slow->Next; if(Fast = =slow) Break; } Fast= fast->Next; Slow=Head;  while(Fast! =slow) {Fast= fast->Next; Slow= slow->Next; }    returnFast;}

Problem:

The length of a ring with a linked list is obtained.

  

Answer:

On the basis of the first two questions.

1) When using Hashtable, the solution process is obvious.

2) When using a fast, slow pointer, we can fix a pointer after the pointer encounters, but another pointer moves. When two hands meet again, it is a circle of distance.

Time complexity O (n), Spatial complexity O (1).

Linked list operation--linked list 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.