概述
将两个链表合并,是面试题中经常遇到的编程题,工作中也会少许用到,难度不大。
下文中是将一个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-
最后
以上就是隐形毛豆为你收集整理的两个链表的合并的全部内容,希望文章能够帮你解决两个链表的合并所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复