Merge two sorted single-Linke list

Source: Internet
Author: User
Tags truncated
Introduction
When solving an algorithm problem, we often encounter this situation: simply solving this problem is not difficult, however, if we still need to consider the time complexity and space complexity while solving the problem, the situation will be a little more complicated. How can we design efficient algorithms for a specific question? By analyzing a specific problem, this article tries to elaborate on the viewpoint that full mining and understanding of the conditions known to the question is the prerequisite for designing an efficient algorithm.

Recently, I saw a single-chain algorithm question on csdn:
"Merge two single links that have been sorted in ascending order into one single link in ascending order"
When I got this question, I consciously followed the known conditions of the question and noticed that it contains two important information:
1. Single-chain. In another article, I have analyzed some of the features of a single link, that is, its iteration operations can only be forward and cannot be backward, you can use additional variables to save the required Node during iteration.
2 .. In ascending order. Because the two single links are already sorted in order, the merging results also require a single link in order. Obviously, this condition may provide meaningful help for our algorithm design.

We can imagine the simplest (and most special) Case: The last node of a single link (that is, the largest node in the single link ), smaller than the first node of another single link (that is, the smallest node in the single link), in this case, the merge of the two single links is very simple: we only need to concatenate these two single links, that is, to point the next pointer of the last node of a single link to the header node of another single link. In this special case, we make full use of the ordering conditions for both single links. Can we use this condition in other more general cases? After a period of thinking, I designed a recursive algorithm based on the recursive characteristics of a single link by taking advantage of the favorable condition that order has been arranged:
1. Declare and initialize some variables:

Node * pheada = plista;/* List */
Node * pheadb = plistb;/* List B */

2. Find the first node that meets the following two conditions in the pheadb single link:

The value of a node is smaller than the header node of a single link pointed to by pheada;
The value of the next node of the node is greater than the header node of the single link pointed by pheada;

3. If such a node can be found. First, disconnect pheadb from this node. At this time, you need to save the node following this node:

Node * pactivenode;/* point to the active node */
Node * ptempnode = pactivenode-> next;

Point the next Of this node to a single link of pheada:

Pactivenode-> next = pheada;

Update the values of pheadb and pheada:

Pheadb = ptempnode;
Ptempnode = pheada;
Pheada = pheada-> next;

There should be three single links:

The remaining parts after pheada is truncated are still represented by pheada;
The remaining parts after pheadb is truncated are still represented by pheadb;
Add the header node of pheada before the change to the front of pheadb, and save the last node of this single link to the ptempnode variable;

The rest is to use recursive methods to merge the remaining parts of pheada and pheadb and connect them to the end of ptempnode:

Ptempnode-> next = Merge (pheada, pheadb );

4. If no such node is found in the pheadb single link:

If the number of nodes in a single link is less than 2
Connect a unique node to an appropriate location in the pheadb single link
If the number of nodes in a single link is greater than (or equal to 2), the maximum node is smaller than the minimum node in another single link.
Connect a single-chain pheada to the front of a Single-Chain pheadb, that is, pheada->...-> pheadb->...
If the number of nodes in a single link is greater than (or equal to 2), the minimum node is larger than the maximum node in another single link.
Connect a single-chain pheada to the end of a Single-Chain pheadb, that is, pheadb->...-> pheada->...
Iteration stops

Postscript
1. This article makes full use of the two single-chain sorting conditions, avoiding a lot of unnecessary comparison and sorting in the process of merging (unnecessary operations are exactly the "moisture" in the algorithm, it is also the place where optimization can be done). At the same time, because of the ease of insertion of a single link, a large number of insert operations are relatively easy during the merge process (simply change some values, ). Although I have not systematically analyzed the average time complexity of the algorithm in this article, I think it should be good. At the same time, because the algorithm only uses a small amount of extra variables (three node pointers), the algorithm in this article also has a good performance in space complexity.
2. In many cases, you can obtain a lot of useful information by analyzing the literal information of a question. However, some problems, useful information, and even the only way to solve the problem are hidden behind the literal information. When we encounter such a problem, if we are frustrated by analyzing the literal information, we may wish to change our thinking or move to a higher level, or convert it to another point (or even the opposite point), maybe we will see a completely different world.

BTW: The above is my personal analysis and understanding. There may be errors, or even serious errors in the algorithms in this article. I hope you can help me to point them out. I am happy to discuss the issues in depth with you.

History
03/31/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.