e-olymp
favorite We need a little bit of your help to keep things running, click on this banner to learn more
Competitions

class LinkedList

LinkedList Intersection

Find the point of intersection of two singly linked lists. Return the pointer to the node where the intersection of two lists starts.

Definition of a single linked list:

// Java
class ListNode {
  int val;
  ListNode next;
  ListNode(int x) {
    val = x;
    next = null;
  }
}
// C++
class ListNode
{
public:
  int val;
  ListNode *next;
  ListNode(int x) : val(x), next(NULL) {}
};

Implement function intersection that returns pointer to the node at which the intersection of two singly linked lists begins.

// Java
ListNode intersection(ListNode l1, ListNode l2)
// C++
ListNode* intersection(ListNode *l1, ListNode *l2)

Example

prb10047.gif

Function intersection must return pointer to the node with value 7.

Time limit 1 second
Memory limit 128 MiB
Author Mykhailo Medvediev