No previous program
No next program

💻 Program Code

/* C Program to concatenate two single linked lists*/

#include<stdio.h>
#include<stdlib.h>

struct node
{
	int info;
	struct node *link;
};
struct node *create_list(struct node *);
struct node *concat( struct node *start1,struct node *start2);
struct node *addatbeg(struct node *start, int data);
struct node *addatend(struct node *start,int data);
void display(struct node *start);

main()
{
	struct node *start1=NULL,*start2=NULL;
	start1=create_list(start1);
	start2=create_list(start2);
	printf("First list is  : ");
	display(start1);
	printf("Second list is  : ");
	display(start2);
    start1=concat(start1, start2);
	printf("Concatenated list is  : ");
	display(start1);
}/*End of main()*/

struct node *concat( struct node *start1,struct node *start2)
{
	struct node *ptr;
	if(start1==NULL)
	{
		start1=start2;
		return start1;
	}
	if(start2==NULL)   
		return start1;
	ptr=start1;
	while(ptr->link!=NULL)
		ptr=ptr->link;
	ptr->link=start2;	
	return start1;
}
struct node *create_list(struct node *start)
{
	int i,n,data;
	printf("Enter the number of nodes : ");
	scanf("%d",&n);
	start=NULL;
	if(n==0)
		return start;

	printf("Enter the element to be inserted : ");
	scanf("%d",&data);
	start=addatbeg(start,data);

	for(i=2;i<=n;i++)
	{
		printf("Enter the element to be inserted : ");
		scanf("%d",&data);
		start=addatend(start,data);	
	}
	return start;
}/*End of create_list()*/

void display(struct node *start)
{
	struct node *p;
	if(start==NULL)
	{
		printf("List is empty\n");
		return;
	}
	p=start;
	while(p!=NULL)
	{
		printf("%d ", p->info);
		p=p->link;
	}
	printf("\n");
}/*End of display() */

struct node *addatbeg(struct node *start,int data)
{
	struct node *tmp;
	tmp=(struct node *)malloc(sizeof(struct node));
	tmp->info=data;
	tmp->link=start;
	start=tmp;
	return start;
}/*End of addatbeg()*/

struct node *addatend(struct node *start, int data)
{
	struct node *p,*tmp;
	tmp= (struct node *)malloc(sizeof(struct node));
	tmp->info=data;
	p=start;
	while(p->link!=NULL)
		p=p->link;
	p->link=tmp;
	tmp->link=NULL;
	return start;
}/*End of addatend()*/
                        

🖥 Program Output

Enter the number of nodes : 3
Enter the element to be inserted : 33
Enter the element to be inserted : 44
Enter the element to be inserted : 55
Enter the number of nodes : 4
Enter the element to be inserted : 12
Enter the element to be inserted : 13
Enter the element to be inserted : 14
Enter the element to be inserted : 45
First list is  : 33 44 55
Second list is  : 12 13 14 45
Concatenated list is  : 33 44 55 12 13 14 45
Press any key to continue . . .
                            

📘 Explanation

C Program to concatenate two single linked lists
No previous program
No next program
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.