Find the first Intersection point in Y Shaped Linked Lists !! using vector and stack
/* Linked List Node
struct Node {
int data;
struct Node *next;
Node(int x) {
data = x;
next = NULL;
}
};
List_A List_B
4 5
| |
1 6
\ /
8 ------ 1
|
4
|
5
|
NULL */
int intersectPoint(Node* head1, Node* head2)
{
vector<Node*>v1,v2;
Node*temp;
temp=head1;
while(temp!=0)
{
v1.push_back(temp->next);
temp=temp->next;
}
temp=head2;
while(temp!=0)
{
v2.push_back(temp->next);
temp=temp->next;
}
stack<int>s;
auto i=v1.size()-2;
auto j=v2.size()-2;
while(v1[i]==v2[j])
{
s.push(v1[i]->data);
i--;j--;
}
return s.top();
}
Comments
Post a Comment