|
| 1 | +#include<stdio.h> |
| 2 | +#include<stdlib.h> |
| 3 | + |
| 4 | +/* Link list node */ |
| 5 | +struct node |
| 6 | +{ |
| 7 | + int data; |
| 8 | + struct node* next; |
| 9 | +}; |
| 10 | + |
| 11 | +/* Function to get the counts of node in a linked list */ |
| 12 | +int getCount(struct node* head); |
| 13 | + |
| 14 | +/* function to get the intersection point of two linked |
| 15 | + lists head1 and head2 where head1 has d more nodes than |
| 16 | + head2 */ |
| 17 | +int _getIntesectionNode(int d, struct node* head1, struct node* head2); |
| 18 | + |
| 19 | +/* function to get the intersection point of two linked |
| 20 | + lists head1 and head2 */ |
| 21 | +int getIntersectionNode(struct node* head1, struct node* head2) |
| 22 | +{ |
| 23 | + int c1 = getCount(head1); |
| 24 | + int c2 = getCount(head2); |
| 25 | + int d; |
| 26 | + |
| 27 | + if(c1 > c2) |
| 28 | + { |
| 29 | + d = c1 - c2; |
| 30 | + return _getIntesectionNode(d, head1, head2); |
| 31 | + } |
| 32 | + else |
| 33 | + { |
| 34 | + d = c2 - c1; |
| 35 | + return _getIntesectionNode(d, head2, head1); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/* function to get the intersection point of two linked |
| 40 | + lists head1 and head2 where head1 has d more nodes than |
| 41 | + head2 */ |
| 42 | +int _getIntesectionNode(int d, struct node* head1, struct node* head2) |
| 43 | +{ |
| 44 | + int i; |
| 45 | + struct node* current1 = head1; |
| 46 | + struct node* current2 = head2; |
| 47 | + |
| 48 | + for(i = 0; i < d; i++) |
| 49 | + { |
| 50 | + if(current1 == NULL) |
| 51 | + { return -1; } |
| 52 | + current1 = current1->next; |
| 53 | + } |
| 54 | + |
| 55 | + while(current1 != NULL && current2 != NULL) |
| 56 | + { |
| 57 | + if(current1 == current2) |
| 58 | + return current1->data; |
| 59 | + current1= current1->next; |
| 60 | + current2= current2->next; |
| 61 | + } |
| 62 | + |
| 63 | + return -1; |
| 64 | +} |
| 65 | + |
| 66 | +/* Takes head pointer of the linked list and |
| 67 | + returns the count of nodes in the list */ |
| 68 | +int getCount(struct node* head) |
| 69 | +{ |
| 70 | + struct node* current = head; |
| 71 | + int count = 0; |
| 72 | + |
| 73 | + while (current != NULL) |
| 74 | + { |
| 75 | + count++; |
| 76 | + current = current->next; |
| 77 | + } |
| 78 | + |
| 79 | + return count; |
| 80 | +} |
| 81 | + |
| 82 | +/* Main func is to quickly test the above function*/ |
| 83 | + |
| 84 | +int main() |
| 85 | +{ |
| 86 | + /* |
| 87 | + Create two linked lists |
| 88 | +
|
| 89 | + 1st 3->6->9->15->30 |
| 90 | + 2nd 10->15->30 |
| 91 | +
|
| 92 | + 15 is the intersection point |
| 93 | + */ |
| 94 | + |
| 95 | + struct node* newNode; |
| 96 | + struct node* head1 = |
| 97 | + (struct node*) malloc(sizeof(struct node)); |
| 98 | + head1->data = 10; |
| 99 | + |
| 100 | + struct node* head2 = |
| 101 | + (struct node*) malloc(sizeof(struct node)); |
| 102 | + head2->data = 3; |
| 103 | + |
| 104 | + newNode = (struct node*) malloc (sizeof(struct node)); |
| 105 | + newNode->data = 6; |
| 106 | + head2->next = newNode; |
| 107 | + |
| 108 | + newNode = (struct node*) malloc (sizeof(struct node)); |
| 109 | + newNode->data = 9; |
| 110 | + head2->next->next = newNode; |
| 111 | + |
| 112 | + newNode = (struct node*) malloc (sizeof(struct node)); |
| 113 | + newNode->data = 15; |
| 114 | + head1->next = newNode; |
| 115 | + head2->next->next->next = newNode; |
| 116 | + |
| 117 | + newNode = (struct node*) malloc (sizeof(struct node)); |
| 118 | + newNode->data = 30; |
| 119 | + head1->next->next= newNode; |
| 120 | + |
| 121 | + head1->next->next->next = NULL; |
| 122 | + |
| 123 | + printf("\n The node of intersection is %d \n", |
| 124 | + getIntersectionNode(head1, head2)); |
| 125 | + |
| 126 | + getchar(); |
| 127 | +} |
0 commit comments