合并两个有序链表——三种方法(递归、循环、二级指针)

//将两个有序链表合并为一个新的有序链表并返回。
//新链表是通过拼接给定的两个链表的所有节点组成的
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>

typedef int DataType;
typedef struct ListNode{
	DataType data;
	struct ListNode *pNext;
}	ListNode;

ListNode *NewSpace(DataType data)
{
	ListNode *pNode = (ListNode *)malloc(sizeof(ListNode));
	assert(pNode);
	pNode->data = data;
	pNode->pNext = NULL;
	return pNode;
}

void InsertBack(ListNode **pHead, DataType data)
{
	if((*pHead) == NULL) {
		(*pHead) = NewSpace(data);
		return;
	}

	ListNode *pInser = (*pHead);
	while(pInser->pNext) {
		pInser = pInser->pNext;
	}
	pInser->pNext = NewSpace(data);
	return;
}

void InsertFront(ListNode **pHead, DataType data)
{
	if((*pHead) == NULL) {
		(*pHead) = NewSpace(data);
	}
	ListNode *pInser = NewSpace(data);
	pInser->pNext = (*pHead);
	(*pHead) = pInser;
}

//**********************************************************************************************************
//**********************************************************************************************************
//合并两个有序链表为新的有序链表
//依次比较大小,小的尾插
//	1.先合并,再排序
//	2.递归
//	3.非递归依次比较进行尾插
//	4.二级指针随时指向新链表的next位置
ListNode *MerTwo(ListNode *Head_1, ListNode *Head_2)
{
	if(Head_1 == NULL || Head_2 == NULL) {
		if(Head_1 == NULL && Head_2 == NULL) {
			return NULL;
		}
		return (Head_1 == NULL) ? Head_2 : Head_1;
	}

	ListNode *pNewList = NULL;
	while(Head_1 && Head_2) {
		if(Head_1->data <= Head_2->data) {
			InsertBack(&pNewList, Head_1->data);
			Head_1 = Head_1->pNext;
		}
		else if(Head_1->data > Head_2->data) {
			InsertBack(&pNewList, Head_2->data);
			Head_2 = Head_2->pNext;
		}
	}

	ListNode *pTmp = pNewList;
	while(pTmp->pNext) {
		pTmp = pTmp->pNext;
	}
	if(Head_1 == NULL || Head_2 == NULL) {
		if(Head_1 == NULL && Head_2 == NULL) {
			return pNewList;
		}
		pTmp->pNext = (Head_1 == NULL) ? Head_2 : Head_1;
	}

	return pNewList;
}

//**********************************************************************************************************
//**********************************************************************************************************
ListNode *TheBestMerge(ListNode *Head_1, ListNode *Head_2)
{
	ListNode *List = NULL;
	ListNode *L1 = Head_1;
	ListNode *L2 = Head_2;
	//二级指针,要改变List链表中的内容
	ListNode **temp = &List;

	while(L1 != NULL || L2 != NULL) {
		if((*temp) == NULL) {
			(*temp) = (ListNode *)malloc(sizeof(ListNode));
			assert(*temp);
			memset(*temp, 0, sizeof(ListNode));
		}

		//当L2为空时会多次执行次语句,知道L1也为空
		//如果L1和L2都不为空且L1的值小于L2的值
		else if(L2 == NULL || (L1 != NULL && L1->data < L2->data)) {
			(*temp)->data = L1->data;
			L1 = L1->pNext;
			temp = &(*temp)->pNext;
		}

		else {
			(*temp)->data = L2->data;
			L2 = L2->pNext;
			temp = &(*temp)->pNext;
		}
	}
	return List;
}

//**********************************************************************************************************
//**********************************************************************************************************
//递归
ListNode* MergeRecursive(ListNode *List_1, ListNode *List_2)
{
	if(List_1 == NULL || List_2 == NULL) {
		if(List_1 == NULL && List_2 == NULL) {
			return NULL;
		}
		else {
			return (List_1 == NULL) ? List_2 : List_1;
		}
	}
	ListNode* NewList = NULL;
	if(List_1->data <= List_2->data) {
		NewList = List_1;
		NewList->pNext = MergeRecursive(List_1->pNext, List_2);
	}
	if(List_1->data > List_2->data) {
		NewList = List_2;
		NewList->pNext = MergeRecursive(List_1, List_2->pNext);
	}
	return NewList;
}
//**********************************************************************************************************
//**********************************************************************************************************

void ListPri(ListNode *pHead)
{
	assert(pHead);

	ListNode *pPri = pHead;
	while(pPri) {
		printf("%d  ", pPri->data);
		pPri = pPri->pNext;
	}
	printf("\n");
	return;
}

int main()
{
	ListNode *List_1 = NULL;
	ListNode *List_2 = NULL;

	InsertBack(&List_1, 1);
	InsertBack(&List_1, 2);
	InsertBack(&List_1, 4);

	InsertBack(&List_2, 1);
	InsertBack(&List_2, 3);
	InsertBack(&List_2, 4);

	ListPri(List_1);
	ListPri(List_2);

	printf("循环\n");
	ListNode *List_3 = MerTwo(List_1, List_2);
	ListPri(List_3);

	printf("最优,二级指针\n");
	ListNode *List_4 = TheBestMerge(List_1, List_2);
	ListPri(List_4);

	printf("递归\n");
	ListNode *List_5 = MergeRecursive(List_1, List_2);
	ListPri(List_5);

	return 0;
}

猜你喜欢

转载自blog.csdn.net/IronMan240/article/details/81879056