Skip to content

Commit b660951

Browse files
committed
Create prob23.c
1 parent 74dbfdb commit b660951

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

prob23.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
Solved by Arulguna, CSE
3+
*/
4+
5+
#include<stdio.h>
6+
#include<stdlib.h>
7+
struct node
8+
{
9+
int data;
10+
struct node* next;
11+
};
12+
int getIntesectionNode(struct node* head1, struct node* head2)
13+
{
14+
int c1 = getCount(head1);
15+
int c2 = getCount(head2);
16+
int d;
17+
if(c1 > c2)
18+
{
19+
d = c1 - c2;
20+
return _getIntesectionNode(d, head1, head2);
21+
}
22+
else
23+
{
24+
d = c2 - c1;
25+
return _getIntesectionNode(d, head2, head1);
26+
}
27+
}
28+
int _getIntesectionNode(int d, struct node* head1, struct node* head2)
29+
{
30+
int i;
31+
struct node* current1 = head1;
32+
struct node* current2 = head2;
33+
for(i = 0; i < d; i++)
34+
{
35+
if(current1 == NULL)
36+
{ return -1; }
37+
current1 = current1->next;
38+
}
39+
while(current1 != NULL && current2 != NULL)
40+
{
41+
if(current1 == current2)
42+
return current1->data;
43+
current1= current1->next;
44+
current2= current2->next;
45+
}
46+
return -1;
47+
}
48+
int getCount(struct node* head)
49+
{
50+
struct node* current = head;
51+
int count = 0;
52+
while (current != NULL)
53+
{
54+
count++;
55+
current = current->next;
56+
}
57+
return count;
58+
}
59+
int main()
60+
{
61+
struct node* newNode;
62+
struct node* head1 =(struct node*) malloc(sizeof(struct node));
63+
head1->data = 10;
64+
struct node* head2 =(struct node*) malloc(sizeof(struct node));
65+
head2->data = 3;
66+
newNode = (struct node*) malloc (sizeof(struct node));
67+
newNode->data = 6;
68+
head2->next = newNode;
69+
newNode = (struct node*) malloc (sizeof(struct node));
70+
newNode->data = 9;
71+
head2->next->next = newNode;
72+
newNode = (struct node*) malloc (sizeof(struct node));
73+
newNode->data = 15;
74+
head1->next = newNode;
75+
head2->next->next->next = newNode;
76+
newNode = (struct node*) malloc (sizeof(struct node));
77+
newNode->data = 30;
78+
head1->next->next= newNode;
79+
head1->next->next->next = NULL;
80+
printf("\n The node of intersection is %d \n",getIntesectionNode(head1, head2));
81+
getchar();
82+
}

0 commit comments

Comments
 (0)