链表-基础

#include<iostream>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
struct Node
{
    Node* next;
    int val;
    Node() {};
    Node(int v)
    {
        val=v;
        next=nullptr;
    }
};

void CreateList(Node* head,int n)
{
    Node* L=head;
    for(int i=0; i<n; i++)
    {
        Node* tmp=new Node(i);
        L->next=tmp;
        L=L->next;
    }
    L->next=nullptr;
}
void PrintList(Node* L)
{
    Node* q;
    q=L->next;//去掉头结点
    while(q)
    {
        cout<<q->val<<" ";
        q=q->next;
    }
    cout<<endl;
}
Merge_List(Node* Head1,Node* Head2)
{
    Node* L3=new Node();
    Node* Head3=L3;
    Node* L1=Head1->next;//去除头结点
    Node* L2=Head2->next;
    while(L1&&L2)
    {
        if(L1->val<=L2->val)
        {
            L3->next=L1;
            L1=L1->next;
        }
        else
        {
            L3->next=L2;
            L2=L2->next;
        }
        L3=L3->next;
    }
    if(L1)
        L3->next=L1;
    else if(L2)
        L3->next=L2;
    PrintList(Head3);
}
int main()
{
    Node* Head1=new Node();
    Node* Head2=new Node();
    CreateList(Head1,5);
    CreateList(Head2,5);
    PrintList(Head1);
    PrintList(Head2);
    Merge_List(Head1,Head2);
    return 0;
}
/**

*/

猜你喜欢

转载自www.cnblogs.com/caijiaming/p/13207300.html
今日推荐