两个有序链表的合并

对于两个有序链表的合并问题,在《剑指offer》中,采用的是如下递归的方式完成的,即:
listNode* merge(listNode* pHead1, listNode* pHead2){
    if(pHead1 == NULL)
        return pHead2;
    else if (pHead2 == NULL)
        return pHead1;
    listNode* pMergedHead = NULL;
    if(pHead1->value < pHead2->value){
        pMergedHead = pHead1;
        pMergedHead->next = merge(pHead1->next, pHead2);
    }else{
        pMergedHead = pHead2;
        pMergedHead->next = merge(pHead1, pHead2->next);
    }
    return pMergedHead;
}

但是,上面算法是不含头结点的。本人写了三种方法,都是带头结点的:

using namespace std;
struct ListNode{
	int data;
	ListNode *next;
};
ListNode* Create_Linklist(int *arr, int len)
{
	ListNode* head = new ListNode();
	head->next = NULL;
	ListNode* L = head;
	for (int i = 0; i < len; i++)
	{
		ListNode* newn = new ListNode();
		newn->data = arr[i];
		newn->next = NULL;
		L->next = newn;
		L = L->next;
	}
	return head;
}
ListNode* Merge1(ListNode* &h1, ListNode* &h2) //新建链表L3作为合并后的链表
{
	if (h1 == NULL || h1->next == NULL)return h2;
	if (h2 == NULL || h2->next == NULL)return h1;
	ListNode* L1 = h1;
	L1 = L1->next;
	ListNode* L2 = h2;
	L2 = L2->next;
	while (L1&&L2)
	{
		if (L1->data <= L2->data)L1 = L1->next;
		else
		{
			ListNode* temp = L2;
			L2 = L2->next;
			temp->next = L1;
			L1 = temp;
		}
	}
	return h1;
}
void Merge2(ListNode* &head, ListNode* h1, ListNode* h2)//链表L1作为合并后的链表
{
	if (h1 == NULL || h1->next == NULL)head = h2;
	if (h2 == NULL || h2->next == NULL)head = h1;
	ListNode* L1 = h1->next;
	ListNode* L2 = h2->next;
	head = new ListNode();
	head->next = NULL;
	ListNode* L = head;
	while (L1 && L2)
	{
		if (L1->data <= L2->data)
		{
			L->next = L1;
			L1 = L1->next;
			L = L->next;
			L->next = NULL;
		}
		else
		{
			L->next = L2;
			L2 = L2->next;
			L = L->next;
			L->next = NULL;
		}
	}
	if (!L1)
	{
		L->next = L2;
	}
	if (!L2)
	{
		L->next = L1;
	}
}
ListNode* Merge3(ListNode* head, ListNode* h1, ListNode* h2)//利用递归实现,注意本人实现的是带头结点的两个有序链表合并
{
	if (h1 == NULL)return h2;
	if (h2 == NULL)return h1;
	ListNode* L1 = h1;
	ListNode* L2 = h2;
	if (L1->data <= L2->data)
	{
		head = L1;
		head->next = Merge3(head, L1->next, L2);
	}
	else
	{
		head = L2;
		head->next = Merge3(head, L1, L2->next);
	}
	return head;
}
void Print_LinkList(ListNode* &head)
{
	if (head)
	{
		ListNode* L = head;
		L = L->next;
		while (L)
		{
			cout << L->data;
			L = L->next;
		}
	}
}
void main(int argc, char *argv[])
{
	int arr1[] = { 1, 3, 5, 7 };
	ListNode* h1 = Create_Linklist(arr1, sizeof(arr1) / sizeof(int));
	int arr2[] = { 2, 4, 6 };
	ListNode* h2 = Create_Linklist(arr2, sizeof(arr2) / sizeof(int));
	//ListNode* merge1 = Merge1(h1, h2);   //链表L1作为合并后的链表
	//ListNode* merge2 = NULL;
	//Merge2(merge2,h1,h2);   //新建链表L3作为合并后的链表
	ListNode* L1 = h1; ListNode* L2 = h2;   //利用递归实现,注意本人实现的是带头结点的两个有序链表合并,因此需要先将L1和L2后移,并用merge3->next带人Merge3函数
	L1 = L1->next; L2 = L2->next;
	ListNode* merge3 = new ListNode();
	merge3->next = NULL;
	merge3->next = Merge3(merge3->next, L1, L2);
	Print_LinkList(merge3);
}


当我们需要合并两个有序链表时,我们可以使用迭代的方式来解决这个问题。首先,我们需要定义一个新的链表头节点,并设置一个指针用于遍历两个链表。然后,我们比较两个指针所指向的节点的值,将较小的节点添加到新链表中,并将指针向后移动。重复这个过程,直到其中一个链表遍历完毕。最后,将剩余链表的节点直接添加到新链表的末尾。 下面是使用C语言实现这个算法的示例代码: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构体 struct ListNode { int val; struct ListNode* next; }; // 创建新节点 struct ListNode* createNode(int val) { struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode)); newNode->val = val; newNode->next = NULL; return newNode; } // 合并两个有序链表 struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) { // 创建一个新的链表头节点 struct ListNode* dummy = createNode(0); // 用于遍历新链表两个原始链表 struct ListNode* cur = dummy; while (l1 != NULL && l2 != NULL) { // 比较两个节点的值 if (l1->val <= l2->val) { cur->next = l1; l1 = l1->next; } else { cur->next = l2; l2 = l2->next; } cur = cur->next; } // 将剩余链表的节点直接添加到新链表的末尾 if (l1 != NULL) { cur->next = l1; } if (l2 != NULL) { cur->next = l2; } return dummy->next; } // 打印链表 void printList(struct ListNode* head) { struct ListNode* cur = head; while (cur != NULL) { printf("%d ", cur->val); cur = cur->next; } printf("\n"); } int main() { // 创建两个有序链表 struct ListNode* l1 = createNode(1); l1->next = createNode(3); l1->next->next = createNode(5); struct ListNode* l2 = createNode(2); l2->next = createNode(4); l2->next->next = createNode(6); // 合并两个有序链表 struct ListNode* mergedList = mergeTwoLists(l1, l2); // 打印合并后的链表 printf("Merged List: "); printList(mergedList); return 0; } ``` 运行上述代码,输出结果为: ``` Merged List: 1 2 3 4 5 6 ``` 这是将两个有序链表合并成一个有序链表的基本思路和实现方法。希望对你有帮助!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值