The sword refers to the offer_ face question 17_ merges two sorts list (two kinds of thinking)

Source: Internet
Author: User
Tags sorts

Title: Enter two incrementally sorted lists, merge the two linked lists, and make the nodes in the new list continue to be sorted in ascending order.

The first thinking: Merging two sorted lists, similar to merging two sorted arrays, differs only by saving data with a linked list, and storing the data in an array.

The algorithm is as follows: (The following algorithm assumes that the head node is not the first data node of the linked list.) )

/** method One: Merge two sorted lists, use the newly created linked list to save, no changes two original linked list */listnode *merge_tow_list (ListNode *phead1,listnode *phead2) {if (NULL = =    PHead1 && NULL = = pHead2) return pHead1;    if (null = = PHead1 && null! = pHead2) return pHead2;    if (null! = PHead1 && NULL = = pHead2) return pHead1;    ListNode *temp1, *TEMP2;        Temp1 = phead1->m_pnext;    /* point to First Data node */Temp2 = phead2->m_pnext;    ListNode *phead_new = new ListNode;    /* Create a new linked list to hold the merged node */phead_new->m_nvalue = 0;    Phead_new->m_pnext = NULL;    ListNode *curr, *temp;    Curr = phead_new;            while (Temp1 = null && TEMP2! = null) {if (Temp1->m_nvalue < Temp2->m_nvalue) {            temp = new ListNode;  Temp->m_nvalue = temp1->m_nvalue;              /* Assign data to a new node */temp->m_pnext = NULL;            /* Add nodes to the new list, followed by the end of the list */curr->m_pnext = temp;            phead_new->m_nvalue++;           Curr = curr->m_pnext; Temp1 = temp1->m_pnext;            } else {temp = new ListNode;            Temp->m_nvalue = temp2->m_nvalue;            Temp->m_pnext = NULL;            Curr->m_pnext = temp;            phead_new->m_nvalue++;            Curr = curr->m_pnext;        Temp2 = temp2->m_pnext;        }} while (NULL! = temp1) {temp = new ListNode;        Temp->m_nvalue = temp1->m_nvalue;        Temp->m_pnext = NULL;        Curr->m_pnext = temp;              phead_new->m_nvalue++;        /* head node Data save node total, each additional data node, this value plus 1*/Curr = curr->m_pnext;    Temp1 = temp1->m_pnext;        } while (NULL! = temp2) {temp = new ListNode;        Temp->m_nvalue = temp2->m_nvalue;        Temp->m_pnext = NULL;        Curr->m_pnext = temp;        phead_new->m_nvalue++;        Curr = curr->m_pnext;    Temp2 = temp2->m_pnext; } return phead_new;}
The following is an algorithm for merging two sorted arrays, which can be seen in the above algorithm:

void Merge_array (int a[], n, int b[], m, int c[]) {    int i, j, K;    i = j = k = 0;    while (I < n && J < m)    {        if (A[i] < b[j])            c[k++] = a[i++];//c[k] = a[i];k++;i++; here J invariant        El Se            c[k++] = b[j++];//c[k] = b[i];k++;j++; Here I do not change    }    //To here there will always be an array to end first, the remainder of the remaining array of data added in turn c[] can be the while    ( I < N)        c[k++] = a[i++];    while (J < m)        c[k++] = b[j++];}
the second kind of thinking: from the book, using recursion:

1, from two linked lists to find the first node, that is, to compare the data values of two linked header nodes, if the first node of the linked list 1 data value is small, then it as the new list of Head Street points;

2, find the second node, is also the head node comparison of two linked lists, at this time, the head node of the list 1 is taken to the next node of the node in the previous step, the head node of the list 2 is still the head node of the original list. Compare these two head nodes, and the small one is taken out and connected to the new linked list head node.

3. Other nodes, and so on ...

The 2nd step, we can find, here can be very convenient to use the principle of recursion to solve.

To solve the problem in this way, it is necessary to have this premise: the first Data node of the list, that is, the head node. (This is different from the premise of the first thought)

The algorithm is as follows

/** second method: using recursion, suitable for the head node is the first Data node linked list representation method *//** code is very concise, clever */listnode * MERGE_TOW_LIST2 (listnode *phead1,listnode *phead2) {    if (NULL = = pHead1)        return pHead2;    else if (NULL = = pHead2)        return pHead1;    ListNode *pmergedhead = NULL;    if (Phead1->m_nvalue < Phead1->m_nvalue)    {        pmergedhead = pHead1;        Pmergedhead->m_pnext = Merge_tow_list2 (phead1->m_pnext,phead2);    }    else    {        pmergedhead = pHead2;        Pmergedhead->m_pnext = Merge_tow_list2 (Phead1,phead2->m_pnext);    }    return pmergedhead;}

The main functions are as follows:(here only the code for the first method is tested)

int main () {    int n_of_list1 = 6;    int n_of_list2 = 3;    int i = 1,j = 2;    ListNode *phead1,*phead2,*phead;    PHead1 = Creat_list ();    while (N_of_list1 > 0)    {        insert_node_behind (phead1,i);        i + = 2;        n_of_list1--;    }    PHead2 = Creat_list ();    while (N_of_list2 > 0)    {        insert_node_behind (phead2,j);        J + = 2;        n_of_list2--;    }    Show_list (PHEAD1);    Show_list (pHead2);    Phead = Merge_tow_list (phead1,phead2);    Show_list (phead);    return 0;}
The results are as follows:(Other test cases can change the data in the main function to get)


/* Little bit of accumulation, my small step O (∩_∩) o~*/

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The sword refers to the offer_ face question 17_ merges two sorts list (two kinds of thinking)

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.