131. 一个链表的结点结构
struct Node
{
int data ;
Node *next ;
};
typedef struct Node Node ;
已知两个链表head1 和head2 各自有序,请把它们合并成一个链表依然有序。
答案:
我自己写的如下
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "iostream"
using namespace std;
struct Node
{
int data ;
Node *next ;
};
typedef struct Node Node ;
void CreatNode(Node** head)
{
int ndata;
Node* headTmp = NULL;
Node* pM = NULL;
Node* pCur = NULL;
pCur = headTmp = (Node*)malloc(sizeof(Node));
headTmp->data = 0;
headTmp->next = NULL;
cin>>ndata;
while ( ndata != 0)
{
pM = (Node*)malloc(sizeof(Node));
pM->data = ndata;
pM->next = NULL;
pCur->next = pM;
pCur = pCur->next;
cin>>ndata;
}
*head = headTmp;
}
Node * Merge(Node *head1 , Node *head2)
{
Node* pCur1 = head1->next;
Node* pCur2 = head2->next;
Node* pResult = (Node*)malloc(sizeof(Node));
Node* pM = NULL;
Node* pCur = pResult;
while (pCur1 && pCur2)
{
int nTmp;
if (pCur1->data > pCur2->data)
{
nTmp = pCur2->data;
pCur2 = pCur2->next;
}
else
{
nTmp = pCur1->data;
pCur1 = pCur1->next;
}
pM = (Node*)malloc(sizeof(Node));
pM->data = nTmp;
pM->next = NULL;
pCur->next = pM;
pCur = pCur->next;
}
while (pCur2)
{
int nTmp = pCur2->data;
pCur2 = pCur2->next;
pM = (Node*)malloc(sizeof(Node));
pM->data = nTmp;
pM->next = NULL;
pCur->next = pM;
pCur = pCur->next;
}
while (pCur1)
{
int nTmp = pCur1->data;
pCur1 = pCur1->next;
pM = (Node*)malloc(sizeof(Node));
pM->data = nTmp;
pM->next = NULL;
pCur->next = pM;
pCur = pCur->next;
}
return pResult;
}
void print(Node *head)
{
Node* tmp = head->next;
while (tmp)
{
cout<<tmp->data<<' ';
tmp = tmp->next;
}
}
int main()
{
Node* head1 = NULL;
Node* head2 = NULL;
CreatNode(&head1);
CreatNode(&head2);
Node * head = Merge(head1, head2);
print(head);
return 1;
}
后来参考答案修改如下
Node * Merge(Node *head1 , Node *head2)
{
if (!head1)
{
return head2;
}
if (!head2)
{
return head1;
}
Node* head = NULL;
Node* pCur1 = head1;
Node* pCur2 = head2;
if (head1->next->data < head2->next->data)
{
head = head1;
pCur1 = head1->next;
}
else
{
head = head2;
pCur2 = head2->next;
}
Node* pCur = head->next;
while (pCur1 && pCur2)
{
if (pCur1->data < pCur2)
{
pCur->next = pCur1;
pCur = pCur->next;
pCur1 = pCur1->next;
}
else
{
pCur->next = pCur2;
pCur = pCur->next;
pCur2 = pCur2->next;
}
}
if (pCur1)
{
pCur->next = pCur1;
}
if (pCur2)
{
pCur->next = pCur2;
}
}
递归方法如下
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "iostream"
using namespace std;
struct Node
{
int data ;
Node *next ;
};
typedef struct Node Node ;
void CreatNode(Node** head)
{
int ndata;
Node* headTmp = NULL;
Node* pM = NULL;
Node* pCur = NULL;
pCur = headTmp = (Node*)malloc(sizeof(Node));
headTmp->data = 0;
headTmp->next = NULL;
cin>>ndata;
while ( ndata != 0)
{
pM = (Node*)malloc(sizeof(Node));
pM->data = ndata;
pM->next = NULL;
pCur->next = pM;
pCur = pCur->next;
cin>>ndata;
}
*head = headTmp;
}
Node* MergeDiDui(Node* head1, Node* head2)
{
if (head1 == NULL)
{
return head2;
}
if (head2 == NULL)
{
return head1;
}
Node* head = NULL;
if (head1->data < head2->data)
{
head = head1;
head->next = MergeDiDui(head1->next, head2);
}
else
{
head = head2;
head->next = MergeDiDui(head1, head2->next);
}
return head;
}
void print(Node *head)
{
Node* tmp = head->next;
while (tmp)
{
cout<<tmp->data<<' ';
tmp = tmp->next;
}
}
int main()
{
Node* head1 = NULL;
Node* head2 = NULL;
CreatNode(&head1);
CreatNode(&head2);
Node * head = MergeDiDui(head1, head2);
print(head);
return 1;
}