knakul853

Untitled

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