Thoughts on single-linked list operations

Source: Internet
Author: User

Preface
The single-linked list is frequently used in a variety of questions and interview questions. The reason for this is that the use of single links is quite extensive, and more importantly, the use of single links is flexible, some single-chain operations have high requirements on the programmer's algorithm analysis and flexible thinking capabilities. This article attempts to find the "universal key" to solve the single-link problem through the analysis of several representative single-link algorithms, at least to provide a general idea.

Single-Chain features
Before entering the specific algorithm analysis, I think it is necessary to analyze the characteristics of a single link that are different from other data structures, more importantly, we need to know the impact of these features on our algorithm analysis (including positive and negative effects ). The structure of a single link is not long-winded here. No data structure book will explain the structure of a single link. Starting from the single link structure, I think the single link traversal process has the following two features:
1> predict the future. For the current node, we can "peek" one or more nodes after this node;
2> difficult to handle. If no record is made for the current node, we will no longer be able to get the node we have experienced.
With these two features, we can feel something vaguely, which is closely related to the algorithm that finally solves the single-link problem. If you don't believe it, let's look at my analysis.

Question 1: reverse a single chain (reverse an single-Linked List)
The biggest problem with reversing a single link is that we need to direct the pointer from the current node to the next node to the one we just accessed, A simple solution is to save all the nodes in a single link, and then access them again in the order different from their access order. For this idea, we can naturally think of using the stack data structure, because the stack's "advanced and later" features are exactly what we need. The algorithm is as follows:
1> Start from the first node, traverse the entire single chain, and push each node to the stack.
2> the first node pops up from the stack and uses the node as the new header node.
3> press the nodes in the stack in sequence and use these nodes as the end nodes of the new chain in order.
4> the entire process continues until the stack is empty.
This algorithm is equivalent to traversing a single link twice, so its time complexity is O (n ). Because it saves all nodes, its space complexity is O (n ). Is there room for optimization for this algorithm? Think about it. In fact, it is not necessary to save all the previously accessed nodes. We only need to save the previous node of the current node. However, when we set the next node of the current node as the previous node, we will lose the next node of the current node. It is not difficult to solve this problem. As long as we save the original next node before adjustment, we still maintain the "ownership" of the original single link after the configuration is complete ". To sum up, we need three additional variables:

Node * pactivenode = phead; // current node
Node * pbehindnode = NULL; // The previous node of the current node
Node * padvancenode = phead-> next; // The next node of the current node

During traversal, we need to set the next node of the current node to the previous one:

Pactivenode-> next = pbehindnode;

At the same time, we need to adjust the three variables in the following order:

Pbehindnode = pactivenode;
Pactivenode = padvancenode;
Padvancenode = padvancenode-> next;

The entire traversal process ends when pactivenode = NULL. pbehindnode is the new header node of a single link.
Compared with the previous algorithms, although their time complexity is O (n), this algorithm only traverses once. The extra memory space used in this algorithm is fixed, so its space complexity is O (1 ). Compared with the previous algorithm, it can be said that the progress is not small.

Question 2: Get the node in the center of a single link
This problem does not seem to be difficult at the beginning. We only need to traverse a single link to obtain the length of this single link. With this length, it is no problem to obtain the node in the middle. The difficulty of this problem is that if we only allow one traversal of this single link, what should we do if we design our algorithm? With the previous experience in solving problem 1, we realized that two additional cursors may be required:

Node * pslownode = phead; // identifies the currently accessed Node
Node * pfastnode = phead; // identifies a node behind the current Access Node

If we set the step of the previous cursor to 1 (one node advances each time), we set the step of the other cursor to 2 (two nodes advance each time ):

Pslownode = pslownode-> next; // forward a node
Pfastnode = pfastnode-> next; // forward two nodes

The forward speed of the subsequent cursor is twice the forward speed of the previous cursor. When the following cursor (quick cursor) reaches the end node (or the first node of the End Node), the cursor (slow cursor) is in the middle of a single link. Aha! This is exactly what we need.

Summary
From the above algorithm analysis, we can usually grasp the following points for Single-Chain traversal:
1> During traversal, we can maintain the following three States:
1> the currently accessed node.
2> the first m of the currently accessed node.
3> the last n nodes of the currently accessed node.
Among them, the first State is required for all traversal processes, and the other two should determine whether to maintain them based on specific problems.
2> During the traversal process, the traversal step size is an important feature of traversal. The selection of correct step size is often the key to algorithm design.
3> During the traversal process, try to avoid multiple traversal. You can use extra memory space to reduce the traversal process to one time in exchange for the Traversal Time.

Postscript
Below I have listed some questions about single-chain links I have collected. You can try to use the solution ideas summarized in this article to think about these issues, which may have unexpected results:
1. Determine whether a single link has a ring (Cyclic single-linked list );
2. Obtain the MTH node (MTH-to-last element of a Single-linked list );
3. Determine whether the single link exists intersection (intersection between two single-linked list );
4. Combine two single links that have been listed in ascending order into one that is still listed in ascending order;
5. One-way linked list deletion operation. Known head, P (pointing to the deleted element), requires complexity of O (1 );
I would like to discuss these issues in depth. If you have any suggestions or questions, please write an email to me.

History
01/09/2007 V1.0
First version of the original article

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.