### C code for finding merge point of two linked lists

//finding the merge point of two lists
//list1 contains 10,15,30
//list 2 contains 3,6,9,15,30 such that
//15 in both the lists are stored in same location ie merge point
/*ALGO:
-count the list1 say c1
-count the list2 say c2
-absoulte difference say d
-traverse the longer list by d nodes
-then traverse both the lists simueltaeously
-when two nodes gets equal in address that is point of merge
*/
#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *next;
};

int main()
{
struct node *head1=(struct node *)malloc(sizeof(struct node));

struct node *head2=(struct node *)malloc(sizeof(struct node));

struct node *newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=6;

newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=9;

newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=15;

newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=30;

printf("\n");
printf("\n");
int c1,c2,d;
printf("\n");
printf("\n");
if(c1>c2)
{
d=c1-c2;
printf("d=c1=%d\n",d);
}
else
{
d=c2-c1;
printf("d=c2=%d\n",d);
}
int t=0;
printf("Merege point is at node:%d\n",t);
return 0;
}
//function to display the nodes
void display(struct node *h)
{
struct node *current=h;
while(current!=NULL)
{
printf("%d ",current->data);
current=current->next;
}

}
//function to count the nodes
int countNode(struct node *h)
{
int count=0;
struct node *current=h;
while(current!=NULL)
{
count=count+1;
current=current->next;
}
printf("size of list1=%d\n",count);

return count;
}
//function to give the merge point
{
int i;
for(i=0;i<d;i++)
{
printf("i=%d,current2->data=%d\n",i,current2->data);
current2=current2->next;
}
while(current1!=NULL && current2!=NULL)
{
printf("while=\n");
if(current1==current2)
{
printf("\n\ncurrent1->data====%d\n\n",current1->data);
return current1->data;
}
current1=current1->next;
current2=current2->next;

}

}

/*OUTPUT:
10 15 30
3 6 9 15 30
size of list1=3

size of list1=5

d=c2=2
i=0,current2->data=3
i=1,current2->data=6
while=
while=
current->data====15
Merege point is at node:15*/