将两个有序链表合并为一个新链表

#include <iostream>
#include <stdio.h>

typedef struct _Node
{
	int value;
	struct _Node *next;
}Node;

Node *MergeList(Node *listA, Node *listB);
void PrintList(Node *head);

int main()
{
	Node lista, nodea, listb, nodeb, nodec;
	lista.value = 2;
	nodea.value = 2;
	nodec.value = 19;
	lista.next = &nodea;
	nodea.next = &nodec;
	nodec.next = NULL;
	listb.value = 3;
	nodeb.value = 4;
	listb.next = &nodeb;
	nodeb.next = NULL;

	Node *listc = MergeList( &lista, &listb );
	PrintList(listc);
	system("pause");
	return 0;
}


/*
@brief merge two sorted lists to a new list which is sorted , too !

@param   in : listA 
@param   in : listB

@return  pHead  the new list of head
*/
Node *MergeList(Node *listA, Node *listB)
{
	Node *pHead = (Node *)malloc(sizeof(Node));
	if ( NULL == pHead )
	{
		printf("error, line %d malloc failed \n", __LINE__);
		return NULL;
	}
	pHead->next = NULL;

	Node *pNode = NULL;
	Node *pLink = pHead;

	// if the list head just is without data use 
	listA = NULL != listA ? listA->next : listA;
	listB = NULL != listB ? listB->next : listB;

	while( NULL != listA && NULL != listB )
	{
		pNode = (Node *)malloc(sizeof(Node));
		if ( NULL == pNode )
		{
			printf("error, line %d malloc failed \n", __LINE__);
			return pHead;
		}

		if ( listA->value <= listB->value )
		{
			pNode->value = listA->value;
			listA = listA->next;
		}
		else
		{
			pNode->value = listB->value;
			listB = listB->next;
		}
		pNode->next = NULL;
		pLink->next = pNode;
		pLink = pLink->next;
	}
			
	while ( NULL != listA )
	{
		pNode = (Node *)malloc(sizeof(Node));
		if ( NULL == pNode )
		{
			printf("error, line %d malloc failed \n", __LINE__);
			return pHead;
		}

		pNode->value = listA->value;
		listA = listA->next;
		pNode->next = NULL;
		pLink->next = pNode;
		pLink = pLink->next;
	}

	while ( NULL != listB )
	{
		pNode = (Node *)malloc(sizeof(Node));
		if ( NULL == pNode )
		{
			printf("error, line %d malloc failed \n", __LINE__);
			return pHead;
		}

		pNode->value = listB->value;
		listB = listB->next;
		pNode->next = NULL;
		pLink->next = pNode;
		pLink = pLink->next;
	}

	return pHead;
}

void PrintList(Node *head)
{
	if ( NULL == head )
	{
		printf("error, list is NULL\n");
		return ;
	}

	// if the list head just is without data use 
	head = head->next;

	while( NULL != head )
	{
		printf("%d\t", head->value);
		head = head->next;
	}
	printf("\n");
}

 

转载于:https://www.cnblogs.com/haihuahuang/p/4273233.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JAVA可以通过以下步骤将两个有序链表合并一个有序链表: 1. 创建一个链表头节点`newNode`和指针`tmp`,用来构建合并后的有序链表。 2. 设置两个链表的头结点分别为`head1`和`head2`。 3. 比较`head1`和`head2`节点的值,将较小的节点连接到`tmp`节点的后面,然后将指针`tmp`指向加入的节点。 4. 移动被选中的节点所在的链表的头指针,即将`head1`或`head2`指向下一个节点。 5. 重复步骤3和步骤4,直到其中一个链表走完。 6. 当其中一个链表走完后,将另一个链表剩下的节点直接连接到`tmp`节点的后面,使其`tmp.next = head1/head2`。 7. 返回链表头节点`newNode.next`即为合并后的有序链表。 通过以上步骤,JAVA可以将两个有序链表合并一个有序链表。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [合并两个有序链表(java)](https://blog.csdn.net/intmainreturn/article/details/128490890)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [合并两个有序链表(Java)](https://blog.csdn.net/m0_63036262/article/details/124417509)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值