题目:输入两个递增排序的链表,合并这两个链表并使新链表中的结点仍然是按照递增排序的。

思路:第一种就是采用递归的方法,先找到第一个最小的,如果是属于第一个链表,然后从第一个链表的下一个元素,继续和第二个链表的第一个元素开始递归寻找。反之亦然。

   第二种就是通过循环遍历的方法,思路还是很清晰的,西安比较两个链表的第一个元素,较小的就是头结点,头结点确定之后继续遍历寻找下一节点,当最终有一个链表先结束,那么剩余的那个链表就是整个链表的尾部。

     话不多说,直接上代码。希望大家好好体会一下,之前还因为粗心,调了好一会程序呢。

     

#include<iostream>
#include<time.h>
#include<stdlib.h>
using namespace std;
struct ListNode
{
    int val;
    struct ListNode* next;
    ListNode(int x) : val(x), next(NULL) { };
};
//第一种方法,递归,相对简单的一种,代码较少
ListNode* Merge1(ListNode* pHead1, ListNode* pHead2)
{
    if (pHead1 == NULL)
        return pHead2;
    if (pHead2 == NULL)
        return pHead1;
    ListNode* MergeHead = NULL;
    if (pHead1->val < pHead2->val)
    {
        MergeHead = pHead1;
        MergeHead->next = Merge1(pHead1->next, pHead2);
    }
    else 
    {
        MergeHead = pHead2;
        MergeHead->next = Merge1(pHead1, pHead2->next);
    }
    return MergeHead;
}
//第二种方法,非递归,用到较多的指针
ListNode* Merge2(ListNode* pHead1, ListNode* pHead2)
{
    if (pHead1 == NULL && pHead2 == NULL)
        return NULL;
    if (pHead1 == NULL && pHead2 != NULL)
        return pHead2;
    if (pHead2 == NULL && pHead1 != NULL)
        return pHead1;
    //确定头结点
    ListNode* mergeHead = NULL;
    if (pHead1->val < pHead2->val)
    {
        mergeHead = pHead1;
        pHead1 = pHead1->next;
    }
    else
    {
        mergeHead = pHead2;
        pHead2 = pHead2->next;
    }
    //接着合并剩余的元素
    ListNode* curElement = mergeHead;
    while (pHead1 != NULL && pHead2 != NULL)
    {
        if (pHead1->val < pHead2->val)
        {
            curElement->next = pHead1;
            curElement = curElement->next;
            pHead1 = pHead1->next;
            
        }
        else
        {
            curElement->next = pHead2;
            curElement = curElement->next;
            pHead2 = pHead2->next;
            
        }
        
    }
    if (pHead1 != NULL)
        curElement->next = pHead1;
    else
        curElement->next = pHead2;
    return mergeHead;
}
//创建链表
/*
ListNode* creatList(int n)
{
    ListNode* head = (ListNode*)malloc(sizeof(ListNode));
    srand(time(0));
    ListNode *p, *q;
    q = head;
    for (int i = 0; i < n; i++)
    {
        p = (ListNode*)malloc(sizeof(ListNode));
        p->val = rand() % 100 + 1;
        q->next = p;
        q = p;
    }
    q->next = NULL;
    return head;
}*/
//连接链表
void connection(ListNode *p1, ListNode* p2)
{
    if (p1 == NULL)
        cout << "The connection is failure";
    p1->next = p2;
}
//销毁链表
void destroyList(ListNode* head)
{
    if (head == NULL)
        cout << "The list is NULL";
    ListNode* node = NULL;
    while (head != NULL)
    {
        ListNode* node = head->next;
        delete head;
        head = node;
    }
}
//打印链表
void print(ListNode* head)
{
    if (head == NULL)
        cout << "The list is NULL" << endl;
    ListNode* p = head;
    while (p != NULL)
    {
        cout << p->val << " ";
        p = p->next;
    }
    cout << endl;
}
int main()
{
    ListNode* m1 = new ListNode(1);
    ListNode* m3 = new ListNode(3);
    ListNode* m5 = new ListNode(5);
    connection(m1, m3);
    connection(m3, m5);
    print(m1);

    ListNode* m2 = new ListNode(2);
    ListNode* m4 = new ListNode(4);
    ListNode* m6 = new ListNode(6);
    connection(m2, m4);
    connection(m4, m6);
    print(m2);
    cout << endl;
/*
    ListNode* p1 = Merge1(m1, m2);
    print(p1);
    destroyList(p1);
*/

    ListNode* p2 = Merge2(m1, m2);
    print(p2);
    destroyList(p2);

    system("pause");
    return 0;
}