Intersection point of two linked lists

1 min read

https://leetcode.com/problems/intersection-of-two-linked-lists/

class Solution {
public:
  ListNode *getIntersectionNode(ListNode *A, ListNode *B) {
    if (A == NULL || B == NULL) {
      return NULL;
    }

    ListNode *a = A;
    ListNode *b = B;

    while (a != b) {
      if (a == NULL) {
        a = B;
      }
      if (b == NULL) {
        b = A;
      }
      if (a == b) {
        break;
      } else {
        a = a->next;
        b = b->next;
      }
    }
    return a;
  }
};
Intersection of two sorted linked list
Merge sort for linked lists