21. The combined ordered list - (list)

21. The combined ordered list - (list)

Subject description: The two ordered lists into a new sorted list and return. The new list is by all nodes in a given mosaic composed of two lists.

Example:

输入:1->2->3, 1->2->3
输出:1->1->2->2->3->3

Code:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
    if (l1 == NULL)
        return l2;
    if (l2 == NULL) 
        return l1;
    
    if (l1->val >= l2->val) {
        l2->next = mergeTwoLists(l1, l2->next);
        return l2;
    } else {
        l1->next = mergeTwoLists(l1->next, l2);
        return l1;
    }
}

Execute the code:

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int val;
    struct Node * next;
} Node;
typedef struct Node * ListNode;

void ShowList(LinkList point);
LinkList mergeTwoLists(LinkList l1, LinkList l2);
void CreateLink(LinkList list, int ch[], int n);

int main(void) {
    LinkList Heada, Headb, point;
    int cha[] = {1, 2, 3};
    int chb[] = {3, 4, 4};

    // 包含头指针的链表...
    Heada = (LinkList) malloc(sizeof(Node));
    Headb = (LinkList) malloc(sizeof(Node));
    point = Heada;
    CreateLink(point, cha, 3);
    ShowList(Heada->next);
    point = Headb;
    CreateLink(point, chb, 3);
    ShowList(Headb->next);

    // unite...
    point = mergeTwoLists(Heada->next, Headb->next);
    ShowList(point);

    return 0;
}

/* 输出显示每条链表 */
void ShowList(LinkList point) {
    while (1) {
        printf("%d", point->value);
        
        if (point->next == NULL)
            break;
        printf("->");
        point = point->next;
    }
    printf("\n");
}

/* 合并两条链表的函数 */
LinkList mergeTwoLists(LinkList l1, LinkList l2) {
    if (l1 == NULL)
        return l2;
    if (l2 == NULL)
        return l1;


    if (l1->value >= l2->value) {
        l2->next = mergeTwoLists(l1, l2->next);
        return l2;
    } else if (l1->value < l2->value) {
        l1->next = mergeTwoLists(l1->next, l2);
        return l1;
    }
}

/* 循环创建链表节点 */
void CreateLink(LinkList list, int ch[], int n) {
    if (list == NULL) {
        printf("LinkList == NULL...\n");
        return;
    }

    LinkList point = list;
    for (int i = 0; i < n; i++) {
        // create New Node...
        LinkList p = (LinkList) malloc(sizeof(Node));
        p->next = NULL;
        p->value = ch[i];
        // link the Node...
        point->next = p;
        point = p;
    }
}

Problem-solving analysis:

  • Two on the list, said that Italy is in an orderly state will have to re-merge into a sorted linked list

    • A (ordered chain) + B (ordered chain) = C (ordered chain)
    • Essentially: the sort! Since it is a "sort", the basic mode of operation is: "Compare"
  • However, because a prerequisite, the two lists are to be merged orderly chain, so the comparison process will simplify many

    • Why do they say, because, in this case, but the order between the two lists is unknown it
    • A long as the value of a linked list element is greater than the value b of the element B in the list, then the former elements that are positioned after the element a is greater than b and certainly those elements. Undoubtedly, a single linked list in ascending or descending can be seen from the example: 1-> 2-> 3, it is clear that small to large. This also reduces, we'll step of comparing (simple +1) on the same linked list. In this already know, follow the A chain is certainly a value greater than the lower case b elements, we can subtract comparison task A subsequent chain elements (simple +1)
  • Design comparison method:

    • From the above analysis, we can know that the comparison need only consider the comparison between different chains.
    • Comparison is specific description: the two chains are set at AB chain, A chain is selected by default smallest element (lowest element) is compared with the smallest element of the B chain. If a greater than or equal to b, then the following we need to do is look at the elements of a b is located after the size of the relationship. If a is less than b, then we need to look at is the element of b is located after the size of a relationship. This whole process is a relatively small loop process, when the end of the above comparison process, we can determine the absolute position of a node in the list after the merge.
    • If the entire list of the combined process is seen as "parent process", then the comparison process described above, it is a "sub-process." = N sub-process whole master process. Finally, because we want to line into a linked list it is, therefore, at the end of each sub-process, should determine the next node connected to the current node. However, due to the characteristics of a single list, we can only choose from front to back are compared, that is to say, when we compare the current node is unable to determine the subsequent node. Because, according to have sex, we are unable to determine what happened back yet, as we say something is going to happen in the future as now. But coding is magic in the world, there is a way to achieve future back, we predict the future spell - recursion.
    • Recursive Design: because the entire process mother = N sub-processes, so the whole comparison process is continuously repeated comparison process loops sub-processes. In addition, we need to know, in order to compare the process for each node is the child process. Based on these analyzes, we can determine the action interval recursive functions are: two different nodes of the list comparing the like; working principle is: first determining the size relationship between the two nodes, and then by determining their subsequent nodes, this step by subsequent nodes in a large value of the node with a smaller size comparison value is compared, in this round of comparison, the larger value of the next round of comparison the like, as the value is smaller by a subsequent connection to other comparable node. Ad infinitum, until when "NULL" to one of the nodes in a peer to peer comparison, the entire recursive began backtracking, finally, over.
      • Here, we need to abstract a few logical steps.
        1. Peer comparison: each node is to compare the determined magnitude relation of the two nodes, the sequence is determined.
        Subsequent link nodes on a smaller value as the 2: Simple is represented in pseudo code, return node small value.
        3. The value of the larger one, and the like is added to the comparison: refer recursive pseudocode represents: a> b {fun (a , b-> next)} || b <a {fun (a-> next, b )}
        4. Press the entire recursion: as for the presence of "NULL" node list means that there come to an end, the foregoing analysis, a list of additional subsequent values are certainly come to the end of the chain is greater than, therefore, direct return. Emergence of return, which means recursive dive ended, back to start.

I stepped pit:

  1. Without careful analysis of the subject entitled to the description of some hidden conditions have not extracted, and began to work on writing code
  2. Have too persistent in the list head node processing mode, resulting in poor applicability written by the function
Published an original article · won praise 0 · Views 24

Guess you like

Origin blog.csdn.net/qq_43483263/article/details/104093263