|
| 1 | +/* Algorithm |
| 2 | +1) Get count of the nodes in the first list, let count be c1. |
| 3 | +2) Get count of the nodes in the second list, let count be c2. |
| 4 | +3) Get the difference of counts d = abs(c1 – c2) |
| 5 | +4) Now traverse the bigger list from the first node till d nodes so that from here onwards both the lists have equal no of nodes. |
| 6 | +5) Then we can traverse both the lists in parallel till we come across a common node. (Note that getting a common node is done by comparing the address of the nodes) |
| 7 | +*/ |
| 8 | + |
| 9 | +#include<stdio.h> |
| 10 | +#include<stdlib.h> |
| 11 | + |
| 12 | +/* Link list node */ |
| 13 | +struct Node |
| 14 | +{ |
| 15 | +int data; |
| 16 | +struct Node* next; |
| 17 | +}; |
| 18 | + |
| 19 | +/* Function to get the counts of node in a linked list */ |
| 20 | +int getCount(struct Node* head); |
| 21 | + |
| 22 | +/* function to get the intersection point of two linked |
| 23 | +lists head1 and head2 where head1 has d more nodes than |
| 24 | +head2 */ |
| 25 | +int _getIntesectionNode(int d, struct Node* head1, struct Node* head2); |
| 26 | + |
| 27 | +/* function to get the intersection point of two linked |
| 28 | +lists head1 and head2 */ |
| 29 | +int getIntesectionNode(struct Node* head1, struct Node* head2) |
| 30 | +{ |
| 31 | +int c1 = getCount(head1); |
| 32 | +int c2 = getCount(head2); |
| 33 | +int d; |
| 34 | + |
| 35 | +if(c1 > c2) |
| 36 | +{ |
| 37 | + d = c1 - c2; |
| 38 | + return _getIntesectionNode(d, head1, head2); |
| 39 | +} |
| 40 | +else |
| 41 | +{ |
| 42 | + d = c2 - c1; |
| 43 | + return _getIntesectionNode(d, head2, head1); |
| 44 | +} |
| 45 | +} |
| 46 | + |
| 47 | +/* function to get the intersection point of two linked |
| 48 | +lists head1 and head2 where head1 has d more nodes than |
| 49 | +head2 */ |
| 50 | +int _getIntesectionNode(int d, struct Node* head1, struct Node* head2) |
| 51 | +{ |
| 52 | +int i; |
| 53 | +struct Node* current1 = head1; |
| 54 | +struct Node* current2 = head2; |
| 55 | + |
| 56 | +for(i = 0; i < d; i++) |
| 57 | +{ |
| 58 | + if(current1 == NULL) |
| 59 | + { return -1; } |
| 60 | + current1 = current1->next; |
| 61 | +} |
| 62 | + |
| 63 | +while(current1 != NULL && current2 != NULL) |
| 64 | +{ |
| 65 | + if(current1 == current2) |
| 66 | + return current1->data; |
| 67 | + current1= current1->next; |
| 68 | + current2= current2->next; |
| 69 | +} |
| 70 | + |
| 71 | +return -1; |
| 72 | +} |
| 73 | + |
| 74 | +/* Takes head pointer of the linked list and |
| 75 | +returns the count of nodes in the list */ |
| 76 | +int getCount(struct Node* head) |
| 77 | +{ |
| 78 | +struct Node* current = head; |
| 79 | +int count = 0; |
| 80 | + |
| 81 | +while (current != NULL) |
| 82 | +{ |
| 83 | + count++; |
| 84 | + current = current->next; |
| 85 | +} |
| 86 | + |
| 87 | +return count; |
| 88 | +} |
| 89 | + |
| 90 | +/* IGNORE THE BELOW LINES OF CODE. THESE LINES |
| 91 | +ARE JUST TO QUICKLY TEST THE ABOVE FUNCTION */ |
| 92 | +int main() |
| 93 | +{ |
| 94 | +/* |
| 95 | + Create two linked lists |
| 96 | +
|
| 97 | + 1st 3->6->9->15->30 |
| 98 | + 2nd 10->15->30 |
| 99 | +
|
| 100 | + 15 is the intersection point |
| 101 | +*/ |
| 102 | + |
| 103 | +struct Node* newNode; |
| 104 | +struct Node* head1 = |
| 105 | + (struct Node*) malloc(sizeof(struct Node)); |
| 106 | +head1->data = 10; |
| 107 | + |
| 108 | +struct Node* head2 = |
| 109 | + (struct Node*) malloc(sizeof(struct Node)); |
| 110 | +head2->data = 3; |
| 111 | + |
| 112 | +newNode = (struct Node*) malloc (sizeof(struct Node)); |
| 113 | +newNode->data = 6; |
| 114 | +head2->next = newNode; |
| 115 | + |
| 116 | +newNode = (struct Node*) malloc (sizeof(struct Node)); |
| 117 | +newNode->data = 9; |
| 118 | +head2->next->next = newNode; |
| 119 | + |
| 120 | +newNode = (struct Node*) malloc (sizeof(struct Node)); |
| 121 | +newNode->data = 15; |
| 122 | +head1->next = newNode; |
| 123 | +head2->next->next->next = newNode; |
| 124 | + |
| 125 | +newNode = (struct Node*) malloc (sizeof(struct Node)); |
| 126 | +newNode->data = 30; |
| 127 | +head1->next->next= newNode; |
| 128 | + |
| 129 | +head1->next->next->next = NULL; |
| 130 | + |
| 131 | +printf("\n The node of intersection is %d \n", |
| 132 | + getIntesectionNode(head1, head2)); |
| 133 | + |
| 134 | +getchar(); |
| 135 | +} |
0 commit comments