fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct node{
  4. int data;
  5. node *next;
  6. };
  7. int print(node *head){
  8. int cnt = 0;
  9. node *q = head;
  10. while(q != NULL){
  11. cnt++;
  12. q = q->next;
  13. }
  14. return cnt;
  15. }
  16. node *addnode(node *head, int val){
  17. node *p = new node();
  18. p->data = val;
  19. p->next = NULL;
  20. return p;
  21. }
  22. node *meeting_point(node *head, node *head1){
  23. int len1 = print(head);
  24. int len2 = print(head1);
  25. if(len1 > len2){
  26. while(len1 > len2)
  27. {
  28. head = head->next;
  29. len1--;
  30. }
  31. }
  32. else if(len1 < len2)
  33. {
  34. while(len2 > len1)
  35. {
  36. head1 = head1->next;
  37. len2--;
  38. }
  39. }
  40. cout<<"Your meeting point is : "<<endl;
  41. while(head != NULL && head1 != NULL){
  42. if(head->data == head1->data)
  43. return head;
  44. else{
  45. head = head->next;
  46. head1 = head1->next;
  47. }
  48. }
  49. return NULL;
  50. }
  51.  
  52. int main(){
  53. node *head = NULL;
  54. head = addnode(head,10);
  55. head->next = addnode(head,20);
  56. head->next->next = addnode(head,30);
  57. head->next->next->next = addnode(head,40);
  58. head->next->next->next->next = addnode(head,50);
  59. head->next->next->next->next->next = addnode(head,60);
  60. head->next->next->next->next->next->next = addnode(head,70);
  61. head->next->next->next->next->next->next->next = addnode(head,80);
  62. head->next->next->next->next->next->next->next->next = addnode(head,90);
  63. head->next->next->next->next->next->next->next->next->next = addnode(head,100);
  64. head->next->next->next->next->next->next->next->next->next->next = addnode(head,101);
  65. head->next->next->next->next->next->next->next->next->next->next->next = addnode(head,102);
  66.  
  67. node *head1 = NULL;
  68. head1 = addnode(head1,1);
  69. head1->next = addnode(head1,2);
  70. head1->next->next = addnode(head1,3);
  71. head1->next->next->next = addnode(head1,4);
  72. head1->next->next->next->next = head->next->next->next->next->next->next->next->next->next;
  73. cout<<endl<<"Your another linked list is : "<<endl;
  74. cout<<meeting_point(head,head1)->data;
  75. return 0;
  76. }
Success #stdin #stdout 0.01s 5504KB
stdin
Standard input is empty
stdout
Your another linked list is : 
Your meeting point is : 
100