diff --git a/Intersection_Point_Of_Two_Linked_List.cpp b/Intersection_Point_Of_Two_Linked_List.cpp new file mode 100644 index 0000000..a05b7f8 --- /dev/null +++ b/Intersection_Point_Of_Two_Linked_List.cpp @@ -0,0 +1,149 @@ +#include +using namespace std; +class node{ + public: + int data; + node* next; + node(int v){ + data=v; + next=NULL; + } +}; + +void insertAtTail(node* &head,int v){ //insert node at the tail of linklist + node* n=new node(v); // make a new node to data is insert + + if (head==NULL) // condition if linklist is empty + { + head=n; + return; + } + + node* temp=head; // make a pointer temp which is point head for traversing the linkedlist + while (temp->next!=NULL) + { + temp=temp->next; + } + + temp->next=n; + } + + +void intersect(node* &head1, node* &head2, int pos)// intersect link list bnai h +{ + node* temp1=head1; + pos--; + while (pos--) + { + temp1=temp1->next; + } + node* temp2 = head2; + while (temp2->next!=NULL) + { + temp2=temp2->next; + } + + temp2->next=temp1; +} + + +int lenght(node* head)// lenght function + { + node* temp=head; + int l=0; + while (temp!=NULL) + { + l++; + temp=temp->next; + } + return l; + } + + +int intersection(node* &head1, node* &head2)// find intersection point +{ +int l1=lenght(head1); +int l2=lenght(head2); +int d=0; +node* ptr1; +node* ptr2; + +if(l1>l2){ +d=l1-l2; +ptr1=head1; +ptr2=head2; +} + +else{ + d=l2-l1; + ptr1=head2; + ptr2=head1; +} + +while (d) +{ + ptr1=ptr1->next; + if(ptr1==NULL) + { + return -1; + } + d--; +} +while (ptr1!=NULL && ptr2!=NULL) +{ + if(ptr1==ptr2) + { + return ptr1->data; + } + + ptr1=ptr1->next; + ptr2=ptr2->next; +} +return -1; +} + + + + + + + + void display(node* head) // display function + { + node* temp=head; + while (temp!=NULL) + { + cout<data<<" -> "; + temp=temp->next; + } + cout<<"NULL"<