xxxxxxxxxx
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
display(struct node *head)
{
if(head == NULL)
{
printf("NULL\n");
}
else
{
printf("%d\n", head -> data);
display(head->next);
}
}
void concatenate(struct node *a,struct node *b)
{
if( a != NULL && b!= NULL )
{
if (a->next == NULL)
a->next = b;
else
concatenate(a->next,b);
}
else
{
printf("Either a or b is NULL\n");
}
}
int main()
{
struct node *prev,*a, *b, *p;
int n,i;
printf ("number of elements in a:");
scanf("%d",&n);
a=NULL;
for(i=0;i<n;i++)
{
p=malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=NULL;
if(a==NULL)
a=p;
else
prev->next=p;
prev=p;
}
printf ("number of elements in b:");
scanf("%d",&n);
b=NULL;
for(i=0;i<n;i++)
{
p=malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=NULL;
if(b==NULL)
b=p;
else
prev->next=p;
prev=p;
}
concatenate(a,b);
return 0;
}
xxxxxxxxxx
class ListNode {
val: number
next: ListNode | null
constructor(val?: number, next?: ListNode | null) {
this.val = (val === undefined ? 0 : val)
this.next = (next === undefined ? null : next)
}
}
function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
if (!list1) return list2
if (!list2) return list1
if (!list1 && !list2) return null
let sorted = new ListNode(), current = sorted
while (list1 && list2) {
if (list1.val < list2.val) {
current.next = list1;
list1 = list1.next
} else {
current.next = list2
list2 = list2.next
}
current = current.next
}
current.next = list1 || list2
return sorted.next
};