jayati

Intersection Point in Y Shaped Linked Lists

Aug 31st, 2023
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.24 KB | None | 0 0
  1. //{ Driver Code Starts
  2. #include<iostream>
  3.  
  4. #include<bits/stdc++.h>
  5. using namespace std;
  6.  
  7. /* Link list Node */
  8. struct Node
  9. {
  10.     int data;
  11.     struct Node *next;
  12.     Node(int x)
  13.     {
  14.         data = x;
  15.         next = NULL;
  16.     }
  17. };
  18.  
  19. int intersectPoint(struct Node* head1, struct Node* head2);
  20.  
  21. vector<int> take(int n){
  22.     vector<int> ans(n);
  23.     for(int i=0;i<n;i++) cin>>ans[i];
  24.     return ans;
  25. }
  26.  
  27. Node* inputList(int size, vector<int> v)
  28. {
  29.     if(size==0) return NULL;
  30.    
  31.     int val = v[0];
  32.    
  33.     Node *head = new Node(val);
  34.     Node *tail = head;
  35.    
  36.     for(int i=0; i<size-1; i++)
  37.     {
  38.         val = v[i+1];
  39.         tail->next = new Node(val);
  40.         tail = tail->next;
  41.     }
  42.    
  43.     return head;
  44. }
  45.  
  46. /* Driver program to test above function*/
  47. int main()
  48. {
  49.     srand(time(0));
  50.     int T,n1,n2,n3;
  51.  
  52.     cin>>T;
  53.     while(T--)
  54.     {
  55.         cin>>n1>>n2>>n3;
  56.        
  57.         int p = rand()%3;
  58.        
  59.         vector<int> v1 = take(n1);
  60.         vector<int> v2 = take(n2);
  61.         vector<int> v3 = take(n3);
  62.        
  63.        
  64.         Node* head1 = NULL;
  65.         Node* head2 = NULL;
  66.         Node* common = NULL;
  67.        
  68.         if(p==0){
  69.             common = inputList(n3, v3);
  70.             head1 = inputList(n1, v1);
  71.             head2 = inputList(n2, v2);
  72.         }
  73.         else if(p==1){
  74.            
  75.             head1 = inputList(n1, v1);
  76.             common = inputList(n3, v3);
  77.             head2 = inputList(n2, v2);
  78.         }
  79.          else{
  80.            
  81.             head1 = inputList(n1, v1);
  82.             head2 = inputList(n2, v2);
  83.             common = inputList(n3, v3);
  84.         }
  85.        
  86.         Node* temp = head1;
  87.         while(temp!=NULL && temp->next != NULL)
  88.             temp = temp->next;
  89.         if(temp!=NULL) temp->next = common;
  90.        
  91.         temp = head2;
  92.         while(temp!=NULL && temp->next != NULL)
  93.             temp = temp->next;
  94.         if(temp!=NULL) temp->next = common;
  95.        
  96.         cout << intersectPoint(head1, head2) << endl;
  97.     }
  98.     return 0;
  99. }
  100.  
  101.  
  102. // } Driver Code Ends
  103.  
  104.  
  105. /* Linked List Node
  106. struct Node {
  107.   int data;
  108.   struct Node *next;
  109.   Node(int x) {
  110.     data = x;
  111.     next = NULL;
  112.   }
  113. }; */
  114.  
  115. //Function to find intersection point in Y shaped Linked Lists.
  116. int intersectPoint(Node* head1, Node* head2)
  117. {
  118.     // Your Code Here
  119.     Node* temp = head1;
  120.     Node* temp1 = head2;
  121.     int l=0,l1=0;
  122.    
  123.     while(temp!=NULL)
  124.     {
  125.         l++;
  126.         temp=temp->next;
  127.     }
  128.     while(temp1!=NULL)
  129.     {
  130.         l1++;
  131.         temp1=temp1->next;
  132.     }
  133.     int d;
  134.     Node* cop;
  135.     if(l>l1)
  136.     {
  137.         d=l-l1;
  138.         temp=head1;
  139.         cop=head2;
  140.     }
  141.     else
  142.     {
  143.         d=l1-l;
  144.         temp=head2;
  145.         cop=head1;
  146.     }
  147.    
  148.    
  149.      for (int i = 0; i < d; i++)
  150.     {
  151.         if(temp==NULL)
  152.         {
  153.             return -1;
  154.         }
  155.         temp=temp->next;
  156.     }
  157.     while (temp != NULL && cop != NULL) {
  158.         if (temp == cop)
  159.             return temp->data;
  160.  
  161.         // Move both the pointers forward
  162.         temp = temp->next;
  163.         cop = cop->next;
  164.     }
  165.    
  166.    
  167.     return -1;
  168.    
  169. }
  170. // https://practice.geeksforgeeks.org/problems/intersection-point-in-y-shapped-linked-lists/
Advertisement
Add Comment
Please, Sign In to add comment