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