{"id":3871,"date":"2021-08-16T13:18:50","date_gmt":"2021-08-16T13:18:50","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=3871"},"modified":"2022-03-10T18:40:31","modified_gmt":"2022-03-10T18:40:31","slug":"intersection-point-of-two-linked-lists","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/intersection-point-of-two-linked-lists\/","title":{"rendered":"Intersection point of two linked lists"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644911356836-intersection%20point_Artboard%206.png\" alt=\"\" \/><\/p>\n<h3>Problem Statement<\/h3>\n<p>Given two singly linked lists, which may or may not intersect each other. Write a function to return the intersection point of the two linked lists. If they don\u2019t intersect, return NULL.<\/p>\n<\/p>\n<h3>Problem Statement Understanding<\/h3>\n<p>What do we mean by the intersection of two linked lists?<\/p>\n<p>By intersection, we mean that the two linked lists merge into one linked list at some node. This means they will have some nodes common from the end.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/example-1.png\" alt=\"\" \/><\/p>\n<p>The above 2 linked lists intersect at the node having value 9.<\/p>\n<p>In this problem, we have to find this intersection point of the two given linked list.<\/p>\n<p>Now that we have understood what we have to do, think of how we can approach this problem?<\/p>\n<h3>Approach 1 (Using hash-set)<\/h3>\n<p>One thing we can do is to traverse one of the linked lists and for each node check if it is present in the other linked list. And we return the first matching node as our intersection point.<\/p>\n<p>Now, how to check for the presence of a node in the linked list?<\/p>\n<p>One approach would be that to check for each node of one of the linked lists, we traverse through the other one completely.<\/p>\n<p>But this method would take a lot of time. So, can we do better to check the presence of a node?<\/p>\n<p>We can use a hash-set to store all nodes of the first linked list and check if any node of the second linked list is there or not. By doing so, we reduce the total time taken with the expense of extra space.<\/p>\n<p>Now that you have an idea of what we are going to do, before moving ahead, try to implement it yourself.<\/p>\n<h3>Algorithm<\/h3>\n<ul>\n<li>Declare a hash-set.<\/li>\n<li>Insert all the nodes of one of the linked lists into the hash-set.<\/li>\n<li>Now, traverse the other linked list, and for each node check if it is present in the hash-set or not.<\/li>\n<li>If the node is present, then that is the required intersection point.<\/li>\n<li>If no node of the second linked list is found in the hash-set, then the two linked lists do not intersect.<\/li>\n<\/ul>\n<h3>Code Implementation<\/h3>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_3873 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_3873 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_3873 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_3873 .wpsm_nav-tabs > li.active > a, #tab_container_3873 .wpsm_nav-tabs > li.active > a:hover, #tab_container_3873 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_3873 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_3873 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_3873 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_3873 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_3873 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_3873 .wpsm_nav-tabs > li > a:hover , #tab_container_3873 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_3873 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_3873 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_3873 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_3873 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_3873 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_3873 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_3873 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_3873 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_3873 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_3873 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_3873 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_3873\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_3873\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_3873_1\" aria-controls=\"tabs_desc_3873_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_3873_2\" aria-controls=\"tabs_desc_3873_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_3873_3\" aria-controls=\"tabs_desc_3873_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_3873_4\" aria-controls=\"tabs_desc_3873_4\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_3873\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_3873_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"c\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include <stdio.h>\r\n#include <stdlib.h>\r\n \r\n\/* Link list node *\/\r\nstruct Node {\r\n    int data;\r\n    struct Node* next;\r\n};\r\n \r\nint getCount(struct Node* head);\r\n \r\nint _getIntesectionNode(int d, struct Node* head1, struct Node* head2);\r\n \r\nint getIntesectionNode(struct Node* head1, struct Node* head2)\r\n{\r\n    int c1 = getCount(head1);\r\n    int c2 = getCount(head2);\r\n    int d;\r\n \r\n    if (c1 > c2) {\r\n        d = c1 - c2;\r\n        return _getIntesectionNode(d, head1, head2);\r\n    }\r\n    else {\r\n        d = c2 - c1;\r\n        return _getIntesectionNode(d, head2, head1);\r\n    }\r\n}\r\n \r\nint _getIntesectionNode(int d, struct Node* head1, struct Node* head2)\r\n{\r\n    int i;\r\n    struct Node* current1 = head1;\r\n    struct Node* current2 = head2;\r\n \r\n    for (i = 0; i < d; i++) {\r\n        if (current1 == NULL) {\r\n            return -1;\r\n        }\r\n        current1 = current1->next;\r\n    }\r\n \r\n    while (current1 != NULL && current2 != NULL) {\r\n        if (current1 == current2)\r\n            return current1->data;\r\n        current1 = current1->next;\r\n        current2 = current2->next;\r\n    }\r\n \r\n    return -1;\r\n}\r\n \r\nint getCount(struct Node* head)\r\n{\r\n    struct Node* current = head;\r\n    int count = 0;\r\n \r\n    while (current != NULL) {\r\n        count++;\r\n        current = current->next;\r\n    }\r\n \r\n    return count;\r\n}\r\n \r\nint main()\r\n{\r\n    \r\n \r\n    struct Node* newNode;\r\n    struct Node* head1 = (struct Node*)malloc(sizeof(struct Node));\r\n    head1->data = 10;\r\n \r\n    struct Node* head2 = (struct Node*)malloc(sizeof(struct Node));\r\n    head2->data = 3;\r\n \r\n    newNode = (struct Node*)malloc(sizeof(struct Node));\r\n    newNode->data = 6;\r\n    head2->next = newNode;\r\n \r\n    newNode = (struct Node*)malloc(sizeof(struct Node));\r\n    newNode->data = 9;\r\n    head2->next->next = newNode;\r\n \r\n    newNode = (struct Node*)malloc(sizeof(struct Node));\r\n    newNode->data = 15;\r\n    head1->next = newNode;\r\n    head2->next->next->next = newNode;\r\n \r\n    newNode = (struct Node*)malloc(sizeof(struct Node));\r\n    newNode->data = 30;\r\n    head1->next->next = newNode;\r\n \r\n    head1->next->next->next = NULL;\r\n \r\n    printf(\"&#92;n The node of intersection is %d &#92;n\",\r\n           getIntesectionNode(head1, head2));\r\n \r\n    getchar();\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_3873_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include&lt;bits stdc++.h&gt;\r\nusing namespace std;\r\n\r\nstruct Node {\r\n    int val;\r\n    Node* next;\r\n\r\n    Node(int value){\r\n        val = value;\r\n        next = NULL;\r\n    }\r\n};\r\n\r\nvoid push_front(Node** head, int new_val){\r\n    Node* new_node = new Node(new_val);\r\n    new_node-&gt;next = *head;\r\n    *head = new_node;\r\n}\r\n\r\nvoid printList(Node* head){\r\n    Node* i = head;\r\n    while(i){\r\n        cout&lt;&lt;i-&gt;val&lt;&lt;&quot; &quot;;\r\n        i = i-&gt;next;\r\n    }\r\n    cout&lt;&lt;&quot;&#92;n&quot;;\r\n}\r\n\r\nNode* get_intersection(Node* l1, Node* l2){\r\n    unordered_set&lt;node*&gt; list1;\r\n    for(Node* i=l1;i!=NULL;i=i-&gt;next){\r\n        list1.insert(i);\r\n    }\r\n    for(Node* i=l2;i!=NULL;i=i-&gt;next){\r\n        if(list1.find(i)!=list1.end()){\r\n            \/\/ we found the intersection\r\n            return i;\r\n        }\r\n    }\r\n    \/\/ if no intersection found\r\n    return NULL;\r\n}\r\n\r\nint main(){\r\n    Node* l1 = NULL;\r\n\r\n    push_front(&amp;l1, 4);\r\n    push_front(&amp;l1, 3);\r\n    push_front(&amp;l1, 2);\r\n    push_front(&amp;l1, 1);\r\n\r\n    Node* l2 = NULL;\r\n\r\n    push_front(&amp;l2, 5);\r\n    push_front(&amp;l2, 7);\r\n\r\n    \/\/ l1-&gt; 1 2 3 4\r\n    \/\/ l2-&gt; 7 5\r\n\r\n    Node* l3 = NULL;\r\n    push_front(&amp;l3, 2);\r\n    push_front(&amp;l3, 7);\r\n    push_front(&amp;l3, 9);\r\n\r\n    Node *i = l1, *j = l2;\r\n    while(i &amp;&amp; i-&gt;next) i = i-&gt;next;\r\n    while(j &amp;&amp; j-&gt;next) j = j-&gt;next;\r\n\r\n    i-&gt;next = l3;\r\n    j-&gt;next = l3;\r\n\r\n    cout&lt;&lt;&quot;l1: &quot;; printList(l1);\r\n    \/\/ 1 2 3 4 9 7 2\r\n    cout&lt;&lt;&quot;l2: &quot;; printList(l2);\r\n    \/\/ 7 5 9 7 2\r\n\r\n    Node *intersection = get_intersection(l1,l2);\r\n\r\n    if(intersection != NULL){\r\n        cout&lt;&lt;&quot;l1 and l2 intersect at : &quot;&lt;&lt;intersection-&gt;val;\r\n    }\r\n    else {\r\n        cout&lt;&lt;&quot;l1 and l2 doesn't intersect at eny point&quot;;\r\n    }\r\n}#include&lt;bits stdc++.h=&quot;&quot;&gt;\r\nusing namespace std;\r\n\r\nstruct Node {\r\n    int val;\r\n    Node* next;\r\n\r\n    Node(int value){\r\n        val = value;\r\n        next = NULL;\r\n    }\r\n};\r\n\r\nvoid push_front(Node** head, int new_val){\r\n    Node* new_node = new Node(new_val);\r\n    new_node-&gt;next = *head;\r\n    *head = new_node;\r\n}\r\n\r\nvoid printList(Node* head){\r\n    Node* i = head;\r\n    while(i){\r\n        cout&lt;&lt;i-&gt;val&lt;&lt;&quot; &quot;;\r\n        i = i-&gt;next;\r\n    }\r\n    cout&lt;&lt;&quot;&#92;n&quot;;\r\n}\r\n\r\nNode* get_intersection(Node* l1, Node* l2){\r\n    unordered_set&lt;node*&gt; list1;\r\n    for(Node* i=l1;i!=NULL;i=i-&gt;next){\r\n        list1.insert(i);\r\n    }\r\n    for(Node* i=l2;i!=NULL;i=i-&gt;next){\r\n        if(list1.find(i)!=list1.end()){\r\n            \/\/ we found the intersection\r\n            return i;\r\n        }\r\n    }\r\n    \/\/ if no intersection found\r\n    return NULL;\r\n}\r\n\r\nint main(){\r\n    Node* l1 = NULL;\r\n\r\n    push_front(&amp;l1, 4);\r\n    push_front(&amp;l1, 3);\r\n    push_front(&amp;l1, 2);\r\n    push_front(&amp;l1, 1);\r\n\r\n    Node* l2 = NULL;\r\n\r\n    push_front(&amp;l2, 5);\r\n    push_front(&amp;l2, 7);\r\n\r\n    \/\/ l1-&gt; 1 2 3 4\r\n    \/\/ l2-&gt; 7 5\r\n\r\n    Node* l3 = NULL;\r\n    push_front(&amp;l3, 2);\r\n    push_front(&amp;l3, 7);\r\n    push_front(&amp;l3, 9);\r\n\r\n    Node *i = l1, *j = l2;\r\n    while(i &amp;&amp; i-&gt;next) i = i-&gt;next;\r\n    while(j &amp;&amp; j-&gt;next) j = j-&gt;next;\r\n\r\n    i-&gt;next = l3;\r\n    j-&gt;next = l3;\r\n\r\n    cout&lt;&lt;&quot;l1: &quot;; printList(l1);\r\n    \/\/ 1 2 3 4 9 7 2\r\n    cout&lt;&lt;&quot;l2: &quot;; printList(l2);\r\n    \/\/ 7 5 9 7 2\r\n\r\n    Node *intersection = get_intersection(l1,l2);\r\n\r\n    if(intersection != NULL){\r\n        cout&lt;&lt;&quot;l1 and l2 intersect at : &quot;&lt;&lt;intersection-&gt;val;\r\n    }\r\n    else {\r\n        cout&lt;&lt;&quot;l1 and l2 doesn't intersect at eny point&quot;;\r\n    }\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_3873_3\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nimport java.util.*;\r\n\r\nclass Node \r\n{\r\n\tint data;\r\n\tNode next;\r\n\tNode(int d)\r\n\t{\r\n\t\tdata = d;\r\n\t\tnext = null;\r\n\t}\r\n}\r\nclass VisitedI\r\n{\r\n    public static void Print(Node n)\r\n\t{\r\n\t\tNode cur = n;\r\n\t\twhile (cur != null) {\r\n\t\t\tSystem.out.print(cur.data + \" \");\r\n\t\t\tcur = cur.next;\r\n\t\t}\r\n\t\tSystem.out.println();\r\n\t}\r\n    \/\/ function to find the intersection of two node\r\n\tpublic static Node MegeNode(Node n1, Node n2)\r\n\t{\r\n\t\t\/\/ define hashset\r\n\t\tHashSet<Node> hs = new HashSet<Node>();\r\n\t\twhile (n1 != null) {\r\n\t\t\ths.add(n1);\r\n\t\t\tn1 = n1.next;\r\n\t\t}\r\n\t\twhile (n2 != null) {\r\n\t\t\tif (hs.contains(n2)) {\r\n\t\t\t\treturn n2;\r\n\t\t\t}\r\n\t\t\tn2 = n2.next;\r\n\t\t}\r\n\t\treturn null;\r\n\t}\r\n\tpublic static void main(String[] args)\r\n\t{\r\n\t\t\/\/ list 1\r\n        Node n1 = new Node(1);\r\n        n1.next = new Node(2);\r\n        n1.next.next = new Node(3);\r\n        n1.next.next.next = new Node(4);\r\n        n1.next.next.next.next = new Node(5);\r\n        n1.next.next.next.next.next = new Node(6);\r\n        n1.next.next.next.next.next.next = new Node(7);\r\n        \/\/ list 2\r\n        Node n2 = new Node(10);\r\n        n2.next = new Node(9);\r\n        n2.next.next = new Node(8);\r\n        n2.next.next.next = n1.next.next.next;\r\n        System.out.println(\"List 1 is :\");\r\n        Print(n1);\r\n        System.out.println(\"List 2 is :\");\r\n        Print(n2);\r\n        System.out.println(\"After merging :\"+MegeNode(n1, n2).data);\r\n\r\n\t\t\r\n\t}\r\n\t\r\n}\r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_3873_4\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"python\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n# Python program to get intersection\r\n# point of two linked list\r\nclass Node :\r\n\tdef __init__(self, d):\r\n\t\tself.data = d\r\n\t\tself.next = None\r\n\r\n# Function to print the list\r\ndef Print(n):\r\n\tcur = n\r\n\twhile (cur != None) :\r\n\t\tprint(cur.data, end=\" \")\r\n\t\tcur = cur.next\r\n\tprint(\"\")\r\n\r\n# Function to find the intersection of two node\r\ndef get_intersection(n1, n2):\r\n\t\r\n\t# Define hashset\r\n\ths = set()\r\n\r\n\twhile (n1 != None):\r\n\t\ths.add(n1)\r\n\t\tn1 = n1.next\r\n\twhile (n2 != None):\r\n\t\tif (n2 in hs):\r\n\t\t\treturn n2\r\n\t\tn2 = n2.next\r\n\t\r\n\treturn None\r\n\r\n\r\n# Driver code\r\n\r\n# list 1\r\nn1 = Node(4)\r\nn1.next = Node(3)\r\nn1.next.next = Node(2)\r\nn1.next.next.next = Node(1)\r\n\r\n# list 2\r\nn2 = Node(7)\r\nn2.next = Node(5)\r\n\r\nn3 = Node(9)\r\nn3.next = Node(7)\r\nn3.next.next = Node(2)\r\n\r\nn1.next.next.next.next = n3\r\nn2.next.next = n3\r\n\r\nPrint(n1)\r\nPrint(n2)\r\n\r\nprint(get_intersection(n1, n2).data)\r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_3873 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_3873 a\"),jQuery(\"#tab-content_3873\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<h3>Output<\/h3>\n<p>l1: 1 2 3 4 9 7 2<br \/>\nl2: 7 5 9 7 2<\/p>\n<p>l1 and l2 intersect at : 9<\/p>\n<p><strong>Time Complexity:<\/strong> O(n+m), as in the worst case when there is no intersection point, we need to traverse both linked lists.<br \/>\n<strong>Space Complexity:<\/strong> O(n), as we are storing all the nodes of the first linked list in a hash-set.<br \/>\nWhere n and m are the numbers of nodes in the first and second linked lists, respectively.<\/p>\n<p>Can we do some improvements?<\/p>\n<h3>Approach 2 (By finding the lengths of two linked lists)<\/h3>\n<p>Yes, we can do some improvements if we know the lengths of the linked lists. Let\u2019s understand this with an example:<\/p>\n<p><strong>Example<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/example-2.png\" alt=\"\" \/><\/p>\n<p>1) Let n and m be the lengths of the 2 linked lists (n = 7, m = 5).<br \/>\n2) Let d be the difference in their lengths (d = 2).<br \/>\n3) Let us look at the distance of the intersection of two linked lists from the beginning. For l1 it is 5 and for l2 it is 3.<br \/>\n4) Now if we move d=2 steps in the larger linked list then we will be at the same distance from the intersection point. Now, if we move together in both the lists, we will meet at the intersection point.<\/p>\n<p>In this approach, we don\u2019t use any extra space.<\/p>\n<p>Now that you have an idea of what we are going to do, before moving ahead, try to implement it yourself.<\/p>\n<h3>Algorithm<\/h3>\n<ul>\n<li>Find the lengths of the two linked lists n and m.<\/li>\n<li>Find their absolute difference d.<\/li>\n<li>Traverse d steps ahead in the larger linked list.<\/li>\n<li>Now traverse both the linked lists together till their nodes become equal.<\/li>\n<li>Nodes will become equal only if they are the same node or they both are NULL.<\/li>\n<li>Once they become equal, we can return the equal node as our intersection point.<\/li>\n<\/ul>\n<h3>Dry Run<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/Intersection-point-of-two-linked-list.png\" alt=\"\" \/><\/p>\n<h3>Code Implementation<\/h3>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_3874 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_3874 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_3874 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_3874 .wpsm_nav-tabs > li.active > a, #tab_container_3874 .wpsm_nav-tabs > li.active > a:hover, #tab_container_3874 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_3874 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_3874 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_3874 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_3874 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_3874 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_3874 .wpsm_nav-tabs > li > a:hover , #tab_container_3874 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_3874 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_3874 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_3874 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_3874 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_3874 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_3874 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_3874 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_3874 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_3874 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_3874 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_3874 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_3874\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_3874\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_3874_1\" aria-controls=\"tabs_desc_3874_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_3874_2\" aria-controls=\"tabs_desc_3874_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_3874_3\" aria-controls=\"tabs_desc_3874_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_3874_4\" aria-controls=\"tabs_desc_3874_4\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_3874\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_3874_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"c\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n \r\n\/* Link list node *\/\r\nstruct Node {\r\n    int data;\r\n    struct Node* next;\r\n};\r\n \r\nint getCount(struct Node* head);\r\n \r\nint _getIntesectionNode(int d, struct Node* head1, struct Node* head2);\r\n \r\nint getIntesectionNode(struct Node* head1, struct Node* head2)\r\n{\r\n    int c1 = getCount(head1);\r\n    int c2 = getCount(head2);\r\n    int d;\r\n \r\n    if (c1 &gt; c2) {\r\n        d = c1 - c2;\r\n        return _getIntesectionNode(d, head1, head2);\r\n    }\r\n    else {\r\n        d = c2 - c1;\r\n        return _getIntesectionNode(d, head2, head1);\r\n    }\r\n}\r\n \r\nint _getIntesectionNode(int d, struct Node* head1, struct Node* head2)\r\n{\r\n    int i;\r\n    struct Node* current1 = head1;\r\n    struct Node* current2 = head2;\r\n \r\n    for (i = 0; i &lt; d; i++) {\r\n        if (current1 == NULL) {\r\n            return -1;\r\n        }\r\n        current1 = current1-&gt;next;\r\n    }\r\n \r\n    while (current1 != NULL &amp;&amp; current2 != NULL) {\r\n        if (current1 == current2)\r\n            return current1-&gt;data;\r\n        current1 = current1-&gt;next;\r\n        current2 = current2-&gt;next;\r\n    }\r\n \r\n    return -1;\r\n}\r\n \r\nint getCount(struct Node* head)\r\n{\r\n    struct Node* current = head;\r\n    int count = 0;\r\n \r\n    while (current != NULL) {\r\n        count++;\r\n        current = current-&gt;next;\r\n    }\r\n \r\n    return count;\r\n}\r\n \r\nint main()\r\n{\r\n    \r\n \r\n    struct Node* newNode;\r\n    struct Node* head1 = (struct Node*)malloc(sizeof(struct Node));\r\n    head1-&gt;data = 10;\r\n \r\n    struct Node* head2 = (struct Node*)malloc(sizeof(struct Node));\r\n    head2-&gt;data = 3;\r\n \r\n    newNode = (struct Node*)malloc(sizeof(struct Node));\r\n    newNode-&gt;data = 6;\r\n    head2-&gt;next = newNode;\r\n \r\n    newNode = (struct Node*)malloc(sizeof(struct Node));\r\n    newNode-&gt;data = 9;\r\n    head2-&gt;next-&gt;next = newNode;\r\n \r\n    newNode = (struct Node*)malloc(sizeof(struct Node));\r\n    newNode-&gt;data = 15;\r\n    head1-&gt;next = newNode;\r\n    head2-&gt;next-&gt;next-&gt;next = newNode;\r\n \r\n    newNode = (struct Node*)malloc(sizeof(struct Node));\r\n    newNode-&gt;data = 30;\r\n    head1-&gt;next-&gt;next = newNode;\r\n \r\n    head1-&gt;next-&gt;next-&gt;next = NULL;\r\n \r\n    printf(&quot;&#92;n The node of intersection is %d &#92;n&quot;,\r\n           getIntesectionNode(head1, head2));\r\n \r\n    getchar();\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_3874_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include&lt;bits stdc++.h=&quot;&quot;&gt;\r\nusing namespace std;\r\n\r\nstruct Node {\r\n    int val;\r\n    Node* next;\r\n\r\n    Node(int value){\r\n        val = value;\r\n        next = NULL;\r\n    }\r\n};\r\n\r\nvoid push_front(Node** head, int new_val){\r\n    Node* new_node = new Node(new_val);\r\n    new_node-&gt;next = *head;\r\n    *head = new_node;\r\n}\r\n\r\nvoid printList(Node* head){\r\n    Node* i = head;\r\n    while(i){\r\n        cout&lt;&lt;i-&gt;val&lt;&lt;&quot; &quot;;\r\n        i = i-&gt;next;\r\n    }\r\n    cout&lt;&lt;&quot;&#92;n&quot;;\r\n}\r\n\r\nint get_length(Node* head){\r\n    int len = 0;\r\n    while(head!=NULL){\r\n        head = head-&gt;next;\r\n        len++;\r\n    }\r\n    return len;\r\n}\r\n\r\nNode* get_intersection(Node* l1, Node* l2){\r\n    \r\n    int n = get_length(l1);\r\n    int m = get_length(l2);\r\n\r\n    int d = abs(n-m);\r\n     \r\n     \/\/ moving d steps ahead in larger linked list\r\n    if(n&gt;m){\r\n        while(d--) l1 = l1-&gt;next;\r\n    } else {\r\n        while(d--) l2 = l2-&gt;next;\r\n    }\r\n\r\n    \/\/ now l1 will become equal to l2\r\n    \/\/ only if they intersect or\r\n    \/\/ when they both become NULL\r\n    while(l1!=l2){\r\n        l1 = l1-&gt;next;\r\n        l2 = l2-&gt;next;\r\n    }\r\n    return l1;\r\n}\r\n\r\nint main(){\r\n    Node* l1 = NULL;\r\n\r\n    push_front(&amp;l1, 4);\r\n    push_front(&amp;l1, 3);\r\n    push_front(&amp;l1, 2);\r\n    push_front(&amp;l1, 1);\r\n\r\n    Node* l2 = NULL;\r\n\r\n    push_front(&amp;l2, 5);\r\n    push_front(&amp;l2, 7);\r\n\r\n    \/\/ l1-&gt; 1 2 3 4\r\n    \/\/ l2-&gt; 7 5\r\n\r\n    Node* l3 = NULL;\r\n    push_front(&amp;l3, 2);\r\n    push_front(&amp;l3, 7);\r\n    push_front(&amp;l3, 9);\r\n\r\n    Node *i = l1, *j = l2;\r\n    while(i &amp;&amp; i-&gt;next) i = i-&gt;next;\r\n    while(j &amp;&amp; j-&gt;next) j = j-&gt;next;\r\n\r\n    i-&gt;next = l3;\r\n    j-&gt;next = l3;\r\n\r\n    cout&lt;&lt;&quot;l1: &quot;; printList(l1);\r\n    \/\/ 1 2 3 4 9 7 2\r\n    cout&lt;&lt;&quot;l2: &quot;; printList(l2);\r\n    \/\/ 7 5 9 7 2\r\n\r\n    Node *intersection = get_intersection(l1,l2);\r\n\r\n    if(intersection != NULL){\r\n        cout&lt;&lt;&quot;l1 and l2 intersect at : &quot;&lt;&lt;intersection-&gt;val;\r\n    }\r\n    else {\r\n        cout&lt;&lt;&quot;l1 and l2 doesn't intersect at eny point&quot;;\r\n    }\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_3874_3\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nclass VisitedII {\r\n\r\n\tNode head1, head2;\r\n\r\n\tstatic class Node {\r\n\r\n\t\tint data;\r\n\t\tNode next;\r\n\r\n\t\tNode(int d)\r\n\t\t{\r\n\t\t\tdata = d;\r\n\t\t\tnext = null;\r\n\t\t}\r\n\t}\r\n\r\n\t\/*function to get the intersection point of two linked\r\n\tlists head1 and head2 *\/\r\n\tint getNode()\r\n\t{\r\n\t\tint c1 = getCount(head1);\r\n\t\tint c2 = getCount(head2);\r\n\t\tint d;\r\n\r\n\t\tif (c1 &gt; c2) {\r\n\t\t\td = c1 - c2;\r\n\t\t\treturn _getIntesectionNode(d, head1, head2);\r\n\t\t}\r\n\t\telse {\r\n\t\t\td = c2 - c1;\r\n\t\t\treturn _getIntesectionNode(d, head2, head1);\r\n\t\t}\r\n\t}\r\n\r\n\t\/* function to get the intersection point of two linked\r\n\tlists head1 and head2 where head1 has d more nodes than\r\n\thead2 *\/\r\n\tint _getIntesectionNode(int d, Node node1, Node node2)\r\n\t{\r\n\t\tint i;\r\n\t\tNode current1 = node1;\r\n\t\tNode current2 = node2;\r\n\t\tfor (i = 0; i &lt; d; i++) {\r\n\t\t\tif (current1 == null) {\r\n\t\t\t\treturn -1;\r\n\t\t\t}\r\n\t\t\tcurrent1 = current1.next;\r\n\t\t}\r\n\t\twhile (current1 != null &amp;&amp; current2 != null) {\r\n\t\t\tif (current1.data == current2.data) {\r\n\t\t\t\treturn current1.data;\r\n\t\t\t}\r\n\t\t\tcurrent1 = current1.next;\r\n\t\t\tcurrent2 = current2.next;\r\n\t\t}\r\n\r\n\t\treturn -1;\r\n\t}\r\n\r\n\t\/*Takes head pointer of the linked list and\r\n\treturns the count of nodes in the list *\/\r\n\tint getCount(Node node)\r\n\t{\r\n\t\tNode current = node;\r\n\t\tint count = 0;\r\n\r\n\t\twhile (current != null) {\r\n\t\t\tcount++;\r\n\t\t\tcurrent = current.next;\r\n\t\t}\r\n\r\n\t\treturn count;\r\n\t}\r\n\r\n\tpublic static void main(String[] args)\r\n\t{\r\n\t\tVisitedII list = new VisitedII();\r\n\r\n\t\t\/\/ creating first linked list\r\n\t\tlist.head1 = new Node(1);\r\n\t\tlist.head1.next = new Node(2);\r\n\t\tlist.head1.next.next = new Node(3);\r\n\t\tlist.head1.next.next.next = new Node(4);\r\n\t\tlist.head1.next.next.next.next = new Node(5);\r\n\r\n\t\t\/\/ creating second linked list\r\n\t\tlist.head2 = new Node(10);\r\n\t\tlist.head2.next = new Node(15);\r\n\t\tlist.head2.next.next = new Node(30);\r\n\r\n\t\tif(list.getNode()!=-1)\r\n        {\r\n            System.out.println(&quot;The intersection of both list is :&quot;+list.getNode());\r\n        }\r\n        else \r\n        {\r\n            System.out.println(&quot;No intersection is possible&quot;);\r\n        }\r\n\t}\r\n}\r\n\r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_3874_4\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"python\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nclass Node :\r\n\tdef __init__(self, d):\r\n\t\tself.data = d\r\n\t\tself.next = None\r\n\r\ndef Print(n):\r\n\tcur = n\r\n\twhile (cur != None) :\r\n\t\tprint(cur.data, end=&quot; &quot;)\r\n\t\tcur = cur.next\r\n\tprint(&quot;&quot;)\r\n\r\ndef get_length(head):\r\n\tlen = 0\r\n\twhile head:\r\n\t\thead = head.next\r\n\t\tlen += 1\r\n\r\n\treturn len\r\n\r\ndef get_intersection(l1, l2):\r\n\t\r\n\tn = get_length(l1)\r\n\tm = get_length(l2)\r\n\td = abs(n - m)\r\n\t\r\n\tif n&gt;m:\r\n\t\twhile (d):\r\n\t\t\tl1 = l1.next\r\n\t\t\td -= 1\r\n\telse:\r\n\t\twhile d:\r\n\t\t\tl2 = l2.next\r\n\t\t\td -= 1\r\n\r\n\twhile l1 != l2:\r\n\t\tl1 = l1.next\r\n\t\tl2 = l2.next\r\n\r\n\treturn l1\r\n\r\n\r\nn1 = Node(4)\r\nn1.next = Node(3)\r\nn1.next.next = Node(2)\r\nn1.next.next.next = Node(1)\r\n\r\nn2 = Node(7)\r\nn2.next = Node(5)\r\n\r\nn3 = Node(9)\r\nn3.next = Node(7)\r\nn3.next.next = Node(2)\r\n\r\nn1.next.next.next.next = n3\r\nn2.next.next = n3\r\n\r\nPrint(n1)\r\nPrint(n2)\r\n\r\nprint(get_intersection(n1, n2).data)\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_3874 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_3874 a\"),jQuery(\"#tab-content_3874\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Time Complexity:<\/strong> O(n+m), as we need to traverse both linked lists to find their lengths.<\/p>\n<p>[forminator_quiz id=&#8221;3872&#8243;]<\/p>\n<p>In this blog, we have seen how to find the intersection point of two linked lists by two different methods. Problems like these are good for strengthening your concepts in LinkedList. I would highly recommend you to practice more such problems from <a href=\"https:\/\/mycode.prepbytes.com\/interview-coding\/practice\/linked-list\">Linked List<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Statement Given two singly linked lists, which may or may not intersect each other. Write a function to return the intersection point of the two linked lists. If they don\u2019t intersect, return NULL. Problem Statement Understanding What do we mean by the intersection of two linked lists? By intersection, we mean that the two [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[125],"tags":[],"class_list":["post-3871","post","type-post","status-publish","format-standard","hentry","category-linked-list"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>learn to find Intersection point of two linked lists in C++<\/title>\n<meta name=\"description\" content=\"Learn to find the intersection point of two linked lists. This article explains how to find the intersection point of two linked lists by two different methods.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/intersection-point-of-two-linked-lists\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"learn to find Intersection point of two linked lists in C++\" \/>\n<meta property=\"og:description\" content=\"Learn to find the intersection point of two linked lists. This article explains how to find the intersection point of two linked lists by two different methods.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/intersection-point-of-two-linked-lists\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-08-16T13:18:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-03-10T18:40:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644911356836-intersection%20point_Artboard%206.png\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/intersection-point-of-two-linked-lists\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/intersection-point-of-two-linked-lists\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Intersection point of two linked lists\",\"datePublished\":\"2021-08-16T13:18:50+00:00\",\"dateModified\":\"2022-03-10T18:40:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/intersection-point-of-two-linked-lists\/\"},\"wordCount\":726,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/intersection-point-of-two-linked-lists\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644911356836-intersection%20point_Artboard%206.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/intersection-point-of-two-linked-lists\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/intersection-point-of-two-linked-lists\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/intersection-point-of-two-linked-lists\/\",\"name\":\"learn to find Intersection point of two linked lists in C++\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/intersection-point-of-two-linked-lists\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/intersection-point-of-two-linked-lists\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644911356836-intersection%20point_Artboard%206.png\",\"datePublished\":\"2021-08-16T13:18:50+00:00\",\"dateModified\":\"2022-03-10T18:40:31+00:00\",\"description\":\"Learn to find the intersection point of two linked lists. This article explains how to find the intersection point of two linked lists by two different methods.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/intersection-point-of-two-linked-lists\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/intersection-point-of-two-linked-lists\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/intersection-point-of-two-linked-lists\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644911356836-intersection%20point_Artboard%206.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644911356836-intersection%20point_Artboard%206.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/intersection-point-of-two-linked-lists\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linked list articles\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/linked-list\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Intersection point of two linked lists\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"learn to find Intersection point of two linked lists in C++","description":"Learn to find the intersection point of two linked lists. This article explains how to find the intersection point of two linked lists by two different methods.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prepbytes.com\/blog\/intersection-point-of-two-linked-lists\/","og_locale":"en_US","og_type":"article","og_title":"learn to find Intersection point of two linked lists in C++","og_description":"Learn to find the intersection point of two linked lists. This article explains how to find the intersection point of two linked lists by two different methods.","og_url":"https:\/\/prepbytes.com\/blog\/intersection-point-of-two-linked-lists\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-08-16T13:18:50+00:00","article_modified_time":"2022-03-10T18:40:31+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644911356836-intersection%20point_Artboard%206.png","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/intersection-point-of-two-linked-lists\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/intersection-point-of-two-linked-lists\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Intersection point of two linked lists","datePublished":"2021-08-16T13:18:50+00:00","dateModified":"2022-03-10T18:40:31+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/intersection-point-of-two-linked-lists\/"},"wordCount":726,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/intersection-point-of-two-linked-lists\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644911356836-intersection%20point_Artboard%206.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/intersection-point-of-two-linked-lists\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/intersection-point-of-two-linked-lists\/","url":"https:\/\/prepbytes.com\/blog\/intersection-point-of-two-linked-lists\/","name":"learn to find Intersection point of two linked lists in C++","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/intersection-point-of-two-linked-lists\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/intersection-point-of-two-linked-lists\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644911356836-intersection%20point_Artboard%206.png","datePublished":"2021-08-16T13:18:50+00:00","dateModified":"2022-03-10T18:40:31+00:00","description":"Learn to find the intersection point of two linked lists. This article explains how to find the intersection point of two linked lists by two different methods.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/intersection-point-of-two-linked-lists\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/intersection-point-of-two-linked-lists\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/intersection-point-of-two-linked-lists\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644911356836-intersection%20point_Artboard%206.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644911356836-intersection%20point_Artboard%206.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/intersection-point-of-two-linked-lists\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Linked list articles","item":"https:\/\/prepbytes.com\/blog\/category\/linked-list\/"},{"@type":"ListItem","position":3,"name":"Intersection point of two linked lists"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/3871","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=3871"}],"version-history":[{"count":6,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/3871\/revisions"}],"predecessor-version":[{"id":7949,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/3871\/revisions\/7949"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=3871"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=3871"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=3871"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}