Guest User

Untitled

a guest
Jan 17th, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.79 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class node {
  5.   public :
  6.     int data;
  7.     node * next;
  8.     node(int data){
  9.         this-> data = data;
  10.         next = NULL;
  11.     }
  12.    
  13.    
  14. };
  15.  
  16. void insertHead(node* &head, int data){
  17.     node * n = new node(data);
  18.     n -> next = head;
  19.     head = n;
  20. }
  21.  
  22. void insert(node * &head, int data){
  23.     node * n = new node(data);
  24.     node * temp = head;
  25.     while(temp -> next != NULL){
  26.         temp = temp -> next;
  27.     }
  28.     temp->next = n;
  29.     n -> next = NULL;
  30.    
  31. }
  32.  
  33. void print(node * &head){
  34.    
  35.     node * temp = head;
  36.     while(temp != NULL){
  37.         cout<<temp->data<<" ";
  38.         temp = temp -> next;
  39.     }
  40.    
  41. }
  42.  
  43. void reverseK(node* &head, int k){
  44.      node * temp = head, *curr = head;
  45.      node *tempPointer = NULL;
  46.      node *prev = NULL;
  47.      node * null_node = NULL;
  48.      int count = 0, flag = 0;
  49.      while(temp != NULL){
  50.          prev = NULL;
  51.          count = 0;
  52.          
  53.          while(temp != NULL){
  54.              temp = curr->next;
  55.              curr->next= prev;
  56.              prev = curr;
  57.              curr = temp;
  58.              count++;
  59.              if(count == 1){
  60.                  tempPointer = prev;
  61.              }
  62.              if(count == k){
  63.                  flag++;
  64.                  break;  
  65.                  count = 0;
  66.              }  
  67.             // cout<<flag<<endl;
  68.              
  69.          }
  70.          
  71.          if(flag == 1){
  72.             null_node = head;
  73.             head =  prev;
  74.             // print(head);
  75.             // cout<<endl;
  76.             //cout<<"**"<<endl;
  77.          }
  78.          else{
  79.              null_node->next = prev;
  80.              null_node = tempPointer;
  81.             //   print(head);
  82.             // cout<<endl;
  83.          }
  84.          
  85.          
  86.      }
  87.    
  88.    
  89.    
  90. }
  91.  
  92. int intersect(node *head, node* Head){
  93.     if(head == NULL || Head == NULL)
  94.         return 0;
  95.    
  96.     node * c1 =NULL, *c2 = NULL;
  97.     c1 = head;
  98.     c2 = Head;
  99.     int flag = 0;
  100.     while(c1 != NULL){
  101.        // cout<<"Hello"<<endl;
  102.         while(c2 != NULL){
  103.             if(c1->next == c2->next)
  104.             {
  105.                 return c1->data;
  106.             }
  107.             c2 = c2->next;
  108.         }
  109.        
  110.         c1 = c1->next;
  111.     }
  112.     return 0;
  113. }
  114.  
  115. int main(){
  116.     int n;
  117.     cin>>n;
  118.    
  119.    node *head = NULL, *Head = NULL;
  120.    int i = 0;    
  121.    for(int i = 0; i < n; i++){
  122.        int a;
  123.        cin>>a;
  124.         if(i == 0)
  125.             insertHead(head, a);
  126.         else
  127.             insert(head, a);
  128.    }
  129.    cin>>n;
  130.    for(int i = 0; i < n; i++){
  131.        int a;
  132.        cin>>a;
  133.         if(i == 0)
  134.             insertHead(Head, a);
  135.         else
  136.             insert(Head, a);
  137.    }
  138.    int data = intersect(head, Head);
  139.    cout<<data;
  140.    // print(head1);
  141.        return 0;
  142.    
  143. }
Add Comment
Please, Sign In to add comment