knakul853

Untitled

Jul 16th, 2020
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. /**
  2. knakul853
  3.  */
  4. class Solution {
  5. public:
  6.     // one more solution could be to use the lenght and after travel equal lenght...travel all pointers      // together
  7.     ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
  8.        
  9.         ListNode* a = headA;
  10.         ListNode* b = headB;
  11.         // let A travels a+c (c is the common distance ) and B travels b+c
  12.         // after assigning the pointers to the other side bothe tarvels a+b+2*c
  13.        
  14.         while( a!= b )
  15.         {
  16.             a = a == NULL ? headB : a->next;
  17.             b = b == NULL ? headA : b->next;
  18.            
  19.         }
  20.        
  21.         return a;
  22.        
  23.        
  24.        
  25.     }
  26. };
Add Comment
Please, Sign In to add comment