两个链表的合并

本文探讨了常见的编程面试题——合并两个链表。通过实例讲解如何创建一个新的C链表,将A链表和B链表有效合并。这个过程在实际工作中也有一定的应用价值。
部署运行你感兴趣的模型镜像

将两个链表合并,是面试题中经常遇到的编程题,工作中也会少许用到,难度不大。

下文中是将一个A链表和B链表合并在新的C链表中。

#include<stdio.h>
#include<stdlib.h>
typedef struct node* List;
struct node{
	int data;
	List next;
};
//合并两个链表到新的链表里面
List ReadList()
{
	List L,t,head;
	int data,N;
	head = (List)malloc(sizeof(struct node));
	if(head == NULL)
	{
		printf("No enough to mallocate!\n");
		exit(0);
	}
	t = head;
	scanf("%d",&N);
	while(N--)
	{
		L = (List)malloc(sizeof(struct node));
		scanf("%d",&data);
		L->data = data;
		t->next = L;
		t = L;
	}
	t->next = NULL;
	
	return head;
}

/*List ReadList()
{
	List L,t,head;
	int N;
	head = (List)malloc(sizeof(struct node));
	head->next = NULL;
	t = head;
	scanf("%d",&N);
	while(N--)
	{
		L = (List)malloc(sizeof(struct node));
		scanf("%d",&(L->data));
		L->next = t->next;
		t->next = L;
		t=L;
	}
	return head;
}*/
List MergeAB(List ha,List hb)
{
 List t1,t2,t3,hc;
 hc = ha;
 t1 = ha->next;
 t2 = hb->next;
 t3 =hc;
 while(t1&&t2)
 {
 	if(t1->data<t2->data)
 	{
 		t3->next = t1;
 		t1 = t1->next;
 		t3 = t3->next;
	 }
	 else
	 {
	 	t3->next = t2;
	 	t2 = t2->next;
	 	t2 = t3->next;
	 }
	
 }
 while(t1)
{
	t3->next = t1;
}
 while(t2)
{
	t3->next = t2;
}	 
	return hc;
}

void PrintHc(List hc)
{
	List t;
	t = hc->next;
    int flag=0;
    if(hc==NULL)
    {
    	printf("NULL");
	}
	while(hc!=NULL)
	{
		if(flag)
		{
			printf(" ");
		}
		else
		{
			flag = 1;
			printf("%d",hc->data);
			hc = hc->next;
		}
	}
	}

int main(void)
{
	List ha,hb,hc;
	ha = ReadList();
	hb = ReadList();
	hc =  MergeAB(ha,hb);
	PrintHc(hc);
	
	return 0;
}

 

您可能感兴趣的与本文相关的镜像

Facefusion

Facefusion

AI应用

FaceFusion是全新一代AI换脸工具,无需安装,一键运行,可以完成去遮挡,高清化,卡通脸一键替换,并且Nvidia/AMD等显卡全平台支持

### 合并两个有序链表并按升序排列 在 C# 中,可以通过递归或迭代的方式实现两个有序链表合并,确保合并后的链表仍保持升序排列。以下是两种实现方式的详细说明。 #### 递归方法 递归方法通过比较两个链表的当前节点值,选择较小的节点作为当前合并后的链表的下一个节点,并递归处理剩余部分: ```csharp public class Solution { public ListNode MergeTwoLists(ListNode l1, ListNode l2) { if (l1 == null) { return l2; } else if (l2 == null) { return l1; } else if (l1.val < l2.val) { l1.next = MergeTwoLists(l1.next, l2); return l1; } else { l2.next = MergeTwoLists(l1, l2.next); return l2; } } } ``` 此方法在每次递归调用中将较小值的节点连接到结果链表中,直到其中一个链表为空为止。此实现方式简洁,但需要注意递归深度可能导致栈溢出的问题 [^2]。 #### 迭代方法 迭代方法通过循环处理两个链表,比较当前节点值并逐步构建结果链表: ```csharp public class Solution { public ListNode MergeTwoLists(ListNode l1, ListNode l2) { ListNode prehead = new ListNode(-1); ListNode prev = prehead; while (l1 != null && l2 != null) { if (l1.val <= l2.val) { prev.next = l1; l1 = l1.next; } else { prev.next = l2; l2 = l2.next; } prev = prev.next; } // 合并后 l1 和 l2 最多只有一个还未被合并完 // 直接将链表末尾指向未合并完的链表即可 prev.next = l1 == null ? l2 : l1; return prehead.next; } } ``` 此方法使用一个虚拟头节点 `prehead` 来简化链表合并的逻辑,同时通过循环逐个比较两个链表的节点值,确保结果链表始终按升序排列 。 ### 相关应用场景 - **合并多个有序链表**:可以使用分治策略,将多个链表两两合并,直到最终得到一个完整的升序链表。 - **动态数据结构**:链表的灵活性使其适合处理动态数据流,例如实时排序和插入操作。 ### 性能分析 递归方法的空间复杂度为 $O(n)$,主要由递归调用栈决定;迭代方法的空间复杂度为 $O(1)$,仅使用额外的指针变量。两者的时间复杂度均为 $O(n)$,其中 $n$ 是两个链表的总长度。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值