{"id":3774,"date":"2021-08-10T12:30:02","date_gmt":"2021-08-10T12:30:02","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=3774"},"modified":"2022-11-22T07:30:44","modified_gmt":"2022-11-22T07:30:44","slug":"the-intersection-of-two-sorted-linked-lists","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/the-intersection-of-two-sorted-linked-lists\/","title":{"rendered":"The intersection of two Sorted Linked Lists"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644926625068-118.The%20intersection%20of%20two%20Sorted%20Linked%20Lists_Artboard%201.png\" alt=\"\" \/><\/p>\n<p>We have already seen how union in linked lists work.Now in the insertion there are two linked lists which are given where we have to traverse through those lists to find a common node which is also equal. Then that node is called an intersecting node. Now in this article lets just look into the approach of intersection of two sorted linked lists <\/p>\n<h2>How to Intersect 2 Sorted Linked List<\/h2>\n<p>In this problem, we are given two sorted linked lists and are asked to print a single linked list that contains the common intersection from the two lists. <\/p>\n<p>Let me make this more clear with an example test case:<br \/>\n<strong>Linked list 1<\/strong>:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/54_1-01.png\" alt=\"\" \/><\/p>\n<p><strong>Linked list 2<\/strong>:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/54_input-2-01.png\" alt=\"\" \/><\/p>\n<p>The resultant Linked List after the intersection of the above lists 1 and 2 will be:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/54_-output-01.png\" alt=\"\" \/><\/p>\n<p>Let&#8217;s first understand the problem statement with the help of an example:<\/p>\n<p>If <strong>Linked list 1<\/strong> = 0\u21923\u21924\u21928 and <strong>Linked list 2<\/strong> = 4\u21928\u21929.<br \/>\nNow to find the intersection of the two linked lists, we will have to find the elements which are common in both the linked list 1 and 2 i.e. element which appear in both the lists.<\/p>\n<ul>\n<li>As we can see that 4 and 8 appear in both the list 1 and list 2, so they will be included in the final intersection list.<\/li>\n<li>0, 3, 9 appears in only one of the two lists, so we will not include them in the final intersection list.<\/li>\n<\/ul>\n<p>So, our final intersection list will be 4 \u2192 8.<\/p>\n<p>If <strong>Linked list 1<\/strong> = 6\u21922\u21923\u21929 and <strong>Linked list 2<\/strong> = 2\u21923\u21929.<\/p>\n<ul>\n<li>As we can see that 2, 3 and 9 appear in both the list 1 and list 2, so they will be included in the final intersection list.<\/li>\n<li>6 appears in only one of the two lists, so we will not include them in the final intersection list.<\/li>\n<\/ul>\n<p>So, our final intersection list will be 2 \u2192 3 \u2192 9.<\/p>\n<p>Now, I think from the above examples, it is clear what we are trying to find in this problem. So next we will try to think how we can approach this problem.<\/p>\n<p>Before jumping to the next section of the blog, try to think how you can approach this problem?<\/p>\n<h2>Approach 1 of intersection of two sorted linked lists<\/h2>\n<p>Suppose our initial linked lists are \u2018a\u2019 and \u2018b\u2019, the basic idea is to take two pointers, each pointing to the head of one of the two linked list &#8216;a&#8217; and &#8216;b&#8217;. Now start traversing the linked lists until one of them is completely traversed and while traversing check:<\/p>\n<ul>\n<li>If both the elements of the lists are equal, then we need to remove both the nodes from the lists and insert the element to the tail of the dummy LinkedList.<\/li>\n<li>Otherwise, we remove the smaller element among both the linked lists.<\/li>\n<\/ul>\n<p>The dummy linked list which we are referring to above, it&#8217;s main idea is to take the help of a dummy node variable that will store our final resultant list (dummy LinkedList). The pointer tail will point to the last node in the resultant list, so that the new nodes can be added easily to the resultant list. This dummy node is used as a temporary node.<\/p>\n<p>When we have traversed either of the two given lists completely, then the result is in the dummy list. The values are allocated from the next node of the dummy, i.e. our resultant intersection list starts from the next of the dummy node (dummy.next).<\/p>\n<h2>Code Implementation of intersection of two sorted linked lists<\/h2>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_3775 {\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_3775 .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_3775 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_3775 .wpsm_nav-tabs > li.active > a, #tab_container_3775 .wpsm_nav-tabs > li.active > a:hover, #tab_container_3775 .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_3775 .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_3775 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_3775 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_3775 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_3775 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_3775 .wpsm_nav-tabs > li > a:hover , #tab_container_3775 .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_3775 .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_3775 .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_3775 .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_3775 .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_3775 .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_3775 .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_3775 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_3775 .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_3775 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_3775 .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_3775 .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_3775\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_3775\">\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_3775_1\" aria-controls=\"tabs_desc_3775_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_3775_2\" aria-controls=\"tabs_desc_3775_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_3775_3\" aria-controls=\"tabs_desc_3775_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_3775_4\" aria-controls=\"tabs_desc_3775_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_3775\">\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_3775_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\nvoid push(struct Node** head_ref, int new_data);\r\n \r\n\/*This solution uses the temporary\r\n dummy to build up the result list *\/\r\nstruct Node* sortedIntersect(\r\n    struct Node* a,\r\n    struct Node* b)\r\n{\r\n    struct Node dummy;\r\n    struct Node* tail = &amp;dummy;\r\n    dummy.next = NULL;\r\n \r\n    \/* Once one or the other\r\n    list runs out -- we're done *\/\r\n    while (a != NULL &amp;&amp; b != NULL) {\r\n        if (a-&gt;data == b-&gt;data) {\r\n            push((&amp;tail-&gt;next), a-&gt;data);\r\n            tail = tail-&gt;next;\r\n            a = a-&gt;next;\r\n            b = b-&gt;next;\r\n        }\r\n        \/* advance the smaller list *\/\r\n        else if (a-&gt;data &lt; b-&gt;data)\r\n            a = a-&gt;next;\r\n        else\r\n            b = b-&gt;next;\r\n    }\r\n    return (dummy.next);\r\n}\r\n \r\n\/* UTILITY FUNCTIONS *\/\r\n\/* Function to insert a node at\r\nthe beginning of the linked list *\/\r\nvoid push(struct Node** head_ref, int new_data)\r\n{\r\n    \/* allocate node *\/\r\n    struct Node* new_node = (struct Node*)malloc(\r\n        sizeof(struct Node));\r\n \r\n    \/* put in the data  *\/\r\n    new_node-&gt;data = new_data;\r\n \r\n    \/* link the old list off the new node *\/\r\n    new_node-&gt;next = (*head_ref);\r\n \r\n    \/* move the head to point to the new node *\/\r\n    (*head_ref) = new_node;\r\n}\r\n \r\n\/* Function to print nodes in\r\n   a given linked list *\/\r\nvoid printList(struct Node* node)\r\n{\r\n    while (node != NULL) {\r\n        printf(&quot;%d &quot;, node-&gt;data);\r\n        node = node-&gt;next;\r\n    }\r\n}\r\n \r\n\/* Driver program to test above functions*\/\r\nint main()\r\n{\r\n    \/* Start with the empty lists *\/\r\n    struct Node* a = NULL;\r\n    struct Node* b = NULL;\r\n    struct Node* intersect = NULL;\r\n \r\n    \/* Let us create the first sorted\r\n     linked list to test the functions\r\n     Created linked list will be\r\n     1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;6 *\/\r\n    push(&amp;a, 6);\r\n    push(&amp;a, 5);\r\n    push(&amp;a, 4);\r\n    push(&amp;a, 3);\r\n    push(&amp;a, 2);\r\n    push(&amp;a, 1);\r\n \r\n    \/* Let us create the second sorted linked list\r\n   Created linked list will be 2-&gt;4-&gt;6-&gt;8 *\/\r\n    push(&amp;b, 8);\r\n    push(&amp;b, 6);\r\n    push(&amp;b, 4);\r\n    push(&amp;b, 2);\r\n \r\n    \/* Find the intersection two linked lists *\/\r\n    intersect = sortedIntersect(a, b);\r\n \r\n    printf(&quot;&#92;n Linked list containing common items of a &amp; b &#92;n &quot;);\r\n    printList(intersect);\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_3775_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\nstruct Node {\r\n    int data;\r\n    Node* next;\r\n};\r\nvoid push(Node** head_ref, int new_data);\r\nNode* intersectfunc(Node* a, Node* b)\r\n{\r\n    Node dummy;\r\n    Node* tail = &amp;dummy;\r\n    dummy.next = NULL;\r\n\r\n    while (a != NULL &amp;&amp; b != NULL) {\r\n        if (a-&gt;data == b-&gt;data) {\r\n            push((&amp;tail-&gt;next), a-&gt;data);\r\n            tail = tail-&gt;next;\r\n            a = a-&gt;next;\r\n            b = b-&gt;next;\r\n        }\r\n        else if (a-&gt;data &lt; b-&gt;data)\r\n            a = a-&gt;next;\r\n        else\r\n            b = b-&gt;next;\r\n    }\r\n    return (dummy.next);\r\n}\r\nvoid push(Node** head_ref, int new_data)\r\n{\r\n    Node* new_node = (Node*)malloc(\r\n        sizeof(Node));\r\n    new_node-&gt;data = new_data;\r\n    new_node-&gt;next = (*head_ref);\r\n    (*head_ref) = new_node;\r\n}\r\nvoid show(Node* node)\r\n{\r\n    while (node != NULL) {\r\n        cout &lt;&lt; node-&gt;data &lt;&lt;&quot; &quot;;\r\n        node = node-&gt;next;\r\n    }\r\n}\r\nint main()\r\n{\r\n    Node* a = NULL;\r\n    Node* b = NULL;\r\n    Node* intersect = NULL;\r\n    push(&amp;a, 8);\r\n    push(&amp;a, 4);\r\n    push(&amp;a, 3);\r\n    push(&amp;a, 0);\r\n    push(&amp;b, 9);\r\n    push(&amp;b, 8);\r\n    push(&amp;b, 4);\r\n    intersect = intersectfunc(a, b);\r\n    show(intersect);\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_3775_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\n\r\nclass IntersectionI\r\n{\r\n\tNode a = null;\r\n    Node b = null;\r\n\tstatic Node dummy = null;\r\n\tstatic Node tail = null;\r\n\t\r\n\tstatic class Node \r\n    {\r\n\t\tint data;\r\n\t\tNode next;\r\n\r\n\t\tNode(int data) {\r\n\t\t\tthis.data = data;\r\n\t\t\tnext = null;\r\n\t\t}\r\n\t}\r\n\tvoid printList(Node start) {\r\n\t\tNode p = start;\r\n\t\twhile (p != null) {\r\n\t\t\tSystem.out.print(p.data + &quot; &quot;);\r\n\t\t\tp = p.next;\r\n\t\t}\r\n\t\tSystem.out.println();\r\n\t}\r\n\tvoid push(int data) {\r\n\t\tNode temp = new Node(data);\r\n\t\tif(dummy == null) {\r\n\t\t\tdummy = temp;\r\n\t\t\ttail = temp;\r\n\t\t}\r\n\t\telse {\r\n\t\t\ttail.next = temp;\r\n\t\t\ttail = temp;\r\n\t\t}\r\n\t}\r\n\t\/\/ function for finding intersection and adding it to dummy list\r\n\tvoid sortedIntersect()\r\n\t{\r\n\t\r\n\t\t\/\/ pointers for iterating\r\n\t\tNode p = a,q = b;\r\n\t\twhile(p != null &amp;&amp; q != null)\r\n\t\t{\r\n\t\t\tif(p.data == q.data)\r\n\t\t\t{\r\n\t\t\t\t\/\/ add to dummy list\r\n\t\t\t\tpush(p.data);\r\n\t\t\t\tp = p.next;\r\n\t\t\t\tq = q.next;\r\n\t\t\t}\r\n\t\t\telse if(p.data &lt; q.data)\r\n\t\t\t\tp = p.next;\r\n\t\t\telse\r\n\t\t\t\tq= q.next;\r\n\t\t}\r\n\t}\r\n\t\/\/ Driver code\r\n\tpublic static void main(String args[])\r\n\t{\r\n\t\tIntersectionI list = new IntersectionI();\r\n\t\t\r\n\t\t\/\/ creating first linked list\r\n\t\tlist.a = new Node(1);\r\n\t\tlist.a.next = new Node(2);\r\n\t\tlist.a.next.next = new Node(3);\r\n\t\tlist.a.next.next.next = new Node(4);\r\n\t\tlist.a.next.next.next.next = new Node(6);\r\n\r\n\t\t\/\/ creating second linked list\r\n\t\tlist.b = new Node(2);\r\n\t\tlist.b.next = new Node(4);\r\n\t\tlist.b.next.next = new Node(6);\r\n\t\tlist.b.next.next.next = new Node(8);\r\n\t\t\r\n\t\t\/\/ function call for intersection\r\n\t\tlist.sortedIntersect();\r\n\t\r\n\t\t\/\/ print required intersection\r\n\t\tSystem.out.println(&quot;Linked list containing common items of a &amp; b&quot;);\r\n\t\tlist.printList(dummy);\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_3775_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):\r\n\t\tself.data = 0\r\n\t\tself.next = None\r\n\t\r\ndef sortedIntersect(a, b):\r\n\tdummy = Node()\r\n\ttail = dummy;\r\n\tdummy.next = None;\r\n\r\n\twhile (a != None and b != None):\r\n\t\tif (a.data == b.data):\r\n\t\t\ttail.next = push((tail.next), a.data);\r\n\t\t\ttail = tail.next;\r\n\t\t\ta = a.next;\r\n\t\t\tb = b.next;\r\n\t\t\r\n\t\telif(a.data &lt; b.data):\r\n\t\t\ta = a.next;\r\n\t\telse:\r\n\t\t\tb = b.next;\r\n\treturn (dummy.next);\r\n\r\ndef push(head_ref, new_data):\r\n\r\n\tnew_node = Node()\r\n\r\n\tnew_node.data = new_data;\r\n\r\n\tnew_node.next = (head_ref);\r\n\r\n\t(head_ref) = new_node;\r\n\treturn head_ref\r\n\r\ndef printList(node):\r\n\twhile (node != None):\r\n\t\tprint(node.data, end=' ')\r\n\t\tnode = node.next;\r\n\t\r\nif __name__=='__main__':\r\n\t\r\n\ta = None;\r\n\tb = None;\r\n\tintersect = None;\r\n\r\n\ta = push(a, 8);\r\n\ta = push(a, 4);\r\n\ta = push(a, 3);\r\n\ta = push(a, 0);\r\n\r\n\tb = push(b, 9);\r\n\tb = push(b, 8);\r\n\tb = push(b, 4);\r\n\r\n\tintersect = sortedIntersect(a, b);\r\n\tprintList(intersect);\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_3775 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_3775 a\"),jQuery(\"#tab-content_3775\"));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<pre><code>Output: 4\u21928<\/code><\/pre>\n<p><strong>Time Complexity for intersection of 2 sorted linked list<br \/>\n:<\/strong> O(m+n), where m and n are the numbers of nodes in the first and second linked lists, respectively.<\/p>\n<p><strong>Space Complexity for intersection of 2 sorted linked list<br \/>\n:<\/strong> O(min(m, n)), as the output list can store at most min(m, n) nodes.<\/p>\n<p>Can we solve the above problem recursively, try to think?<\/p>\n<h2>Approach 2 of  intersection of two sorted linked lists<\/h2>\n<p>I can think of another approach using recursion. It will work the same way, but with a recursive function that takes two nodes and returns the intersected node list. <\/p>\n<p>The most efficient idea is to compare the first elements of both the lists:<\/p>\n<ul>\n<li>If they are similar, then we call the recursive function with the next node of both the lists and create a node with the common node data and return it. <\/li>\n<li>Else, if they are not similar, then we remove the smaller node of both the lists and again call the recursive function.<\/li>\n<\/ul>\n<h2>Algorithm of intersection of two sorted linked lists<\/h2>\n<p>Start comparing the first elements of the two linked lists:<\/p>\n<ul>\n<li>If two elements are similar, then create a new node with the common element and return that node and call the recursive function with the next nodes of the two lists.<\/li>\n<li>If they are different, then remove the smaller node and call the recursive function again.<\/li>\n<\/ul>\n<h3>Dry Run of intersection of two sorted linked lists<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/54_dry-run-01.png\" alt=\"\" \/><\/p>\n<h2>Code Implementation<\/h2>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_3780 {\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_3780 .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_3780 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_3780 .wpsm_nav-tabs > li.active > a, #tab_container_3780 .wpsm_nav-tabs > li.active > a:hover, #tab_container_3780 .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_3780 .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_3780 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_3780 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_3780 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_3780 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_3780 .wpsm_nav-tabs > li > a:hover , #tab_container_3780 .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_3780 .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_3780 .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_3780 .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_3780 .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_3780 .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_3780 .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_3780 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_3780 .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_3780 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_3780 .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_3780 .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_3780\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_3780\">\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_3780_1\" aria-controls=\"tabs_desc_3780_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_3780_2\" aria-controls=\"tabs_desc_3780_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_3780_3\" aria-controls=\"tabs_desc_3780_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_3780_4\" aria-controls=\"tabs_desc_3780_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_3780\">\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_3780_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\nstruct Node* sortedIntersect(\r\n    struct Node* a,\r\n    struct Node* b)\r\n{\r\n    \/* base case *\/\r\n    if (a == NULL || b == NULL)\r\n        return NULL;\r\n \r\n    \/* If both lists are non-empty *\/\r\n \r\n    \/* advance the smaller list and call recursively *\/\r\n    if (a-&gt;data &lt; b-&gt;data)\r\n        return sortedIntersect(a-&gt;next, b);\r\n \r\n    if (a-&gt;data &gt; b-&gt;data)\r\n        return sortedIntersect(a, b-&gt;next);\r\n \r\n    \/\/ Below lines are executed only\r\n    \/\/ when a-&gt;data == b-&gt;data\r\n    struct Node* temp\r\n        = (struct Node*)malloc(\r\n            sizeof(struct Node));\r\n    temp-&gt;data = a-&gt;data;\r\n \r\n    \/* advance both lists and call recursively *\/\r\n    temp-&gt;next = sortedIntersect(a-&gt;next, b-&gt;next);\r\n    return temp;\r\n}\r\n \r\n\/* UTILITY FUNCTIONS *\/\r\n\/* Function to insert a node at\r\nthe beginning of the linked list *\/\r\nvoid push(struct Node** head_ref, int new_data)\r\n{\r\n    \/* allocate node *\/\r\n    struct Node* new_node\r\n        = (struct Node*)malloc(\r\n            sizeof(struct Node));\r\n \r\n    \/* put in the data  *\/\r\n    new_node-&gt;data = new_data;\r\n \r\n    \/* link the old list off the new node *\/\r\n    new_node-&gt;next = (*head_ref);\r\n \r\n    \/* move the head to point to the new node *\/\r\n    (*head_ref) = new_node;\r\n}\r\n \r\n\/* Function to print nodes in a given linked list *\/\r\nvoid printList(struct Node* node)\r\n{\r\n    while (node != NULL) {\r\n        printf(&quot;%d &quot;, node-&gt;data);\r\n        node = node-&gt;next;\r\n    }\r\n}\r\n \r\n\/* Driver program to test above functions*\/\r\nint main()\r\n{\r\n    \/* Start with the empty lists *\/\r\n    struct Node* a = NULL;\r\n    struct Node* b = NULL;\r\n    struct Node* intersect = NULL;\r\n \r\n    \/* Let us create the first sorted\r\n      linked list to test the functions\r\n     Created linked list will be\r\n      1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;6 *\/\r\n    push(&amp;a, 6);\r\n    push(&amp;a, 5);\r\n    push(&amp;a, 4);\r\n    push(&amp;a, 3);\r\n    push(&amp;a, 2);\r\n    push(&amp;a, 1);\r\n \r\n    \/* Let us create the second sorted linked list\r\n     Created linked list will be 2-&gt;4-&gt;6-&gt;8 *\/\r\n    push(&amp;b, 8);\r\n    push(&amp;b, 6);\r\n    push(&amp;b, 4);\r\n    push(&amp;b, 2);\r\n \r\n    \/* Find the intersection two linked lists *\/\r\n    intersect = sortedIntersect(a, b);\r\n \r\n    printf(&quot;&#92;n Linked list containing common items of a &amp; b &#92;n &quot;);\r\n    printList(intersect);\r\n \r\n    return 0;\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_3780_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\nstruct Node\r\n{\r\n    int data;\r\n    struct Node* next;\r\n};\r\nstruct Node* intersectfunc(struct Node* a,    struct Node* b)\r\n{\r\n    if (a == NULL || b == NULL)\r\n        return NULL;\r\n    if (a-&gt;data &lt; b-&gt;data)\r\n        return intersectfunc(a-&gt;next, b);\r\n\r\n    if (a-&gt;data &gt; b-&gt;data)\r\n        return intersectfunc(a, b-&gt;next);\r\n    struct Node* temp = new Node();\r\n    temp-&gt;data = a-&gt;data;\r\n    temp-&gt;next = intersectfunc(a-&gt;next,b-&gt;next);\r\n    return temp;\r\n}\r\nvoid push(struct Node** head_ref, int new_data)\r\n{\r\n    struct Node* new_node = new Node();\r\n    new_node-&gt;data = new_data;\r\n    new_node-&gt;next = (*head_ref);\r\n    (*head_ref) = new_node;\r\n}\r\nvoid show(struct Node* node)\r\n{\r\n    while (node != NULL)\r\n    {\r\n        cout &lt;&lt; &quot; &quot; &lt;&lt; node-&gt;data;\r\n        node = node-&gt;next;\r\n    }\r\n}\r\nint main()\r\n{\r\n    struct Node* a = NULL;\r\n    struct Node* b = NULL;\r\n    struct Node* intersect = NULL;\r\n    push(&amp;a, 8);\r\n    push(&amp;a, 4);\r\n    push(&amp;a, 3);\r\n    push(&amp;a, 0);\r\n\r\n    push(&amp;b, 9);\r\n    push(&amp;b, 8);\r\n    push(&amp;b, 4);\r\n    intersect = intersectfunc(a, b);\r\n    show(intersect);\r\n    return 0;\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_3780_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 IntersectionII\r\n{\r\n    static class Node\r\n    {\r\n\t    int data;\r\n\t    Node next;\r\n    };\r\n    static Node sortedIntersect(Node a,Node b)\r\n    {\r\n\t    \/\/ base case\r\n\t    if (a == null || b == null)\r\n\t\t    return null;\r\n\r\n\t    \/* If both lists are non-empty *\/\r\n\r\n\t    \/* Advance the smaller list and call recursively *\/\r\n\t    if (a.data &lt; b.data)\r\n\t\t    return sortedIntersect(a.next, b);\r\n\r\n\t    if (a.data &gt; b.data)\r\n\t\t    return sortedIntersect(a, b.next);\r\n\r\n\t    \/\/ Below lines are executed only when a.data == b.data\r\n\t    Node temp = new Node();\r\n\t    temp.data = a.data;\r\n\r\n\t    \/\/ Advance both lists and call recursively\r\n\t    temp.next = sortedIntersect(a.next,b.next);\r\n\t    return temp;\r\n    }\r\n    static Node push(Node head_ref, int new_data)\r\n    {\r\n\t    Node new_node = new Node();\r\n        new_node.data = new_data;\r\n        new_node.next = head_ref;\r\n        head_ref = new_node;\r\n\t    return head_ref;\r\n    }\r\n    static void printList(Node node)\r\n    {\r\n\t    while (node != null)\r\n\t    {\r\n\t\t    System.out.print(&quot; &quot; + node.data);\r\n\t\t    node = node.next;\r\n\t    }\r\n    }\r\n    \/\/ Driver code\r\n    public static void main(String[] args)\r\n    {\r\n\t    \/* Start with the empty lists *\/\r\n\t    Node a = null;\r\n\t    Node b = null;\r\n\t    Node intersect = null;\r\n\r\n\t    a = push(a, 6);\r\n\t    a = push(a, 5);\r\n\t    a = push(a, 4);\r\n\t    a = push(a, 3);\r\n\t    a = push(a, 2);\r\n\t    a = push(a, 1);\r\n\r\n\t    \/* Let us create the second sorted linked list\r\n\t    Created linked list will be 2.4.6.8 *\/\r\n\t    b = push(b, 8);\r\n\t    b = push(b, 6);\r\n\t    b = push(b, 4);\r\n\t    b = push(b, 2);\r\n\r\n\t    \/* Find the intersection two linked lists *\/\r\n\t    intersect = sortedIntersect(a, b);\r\n\r\n\t    System.out.print(&quot;&#92;n Linked list containing &quot;+ &quot;common items of a &amp; b &#92;n &quot;);\r\n\t    printList(intersect);\r\n    }\r\n}\r\n\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_3780_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):\r\n\t\tself.data = 0\r\n\t\tself.next = None\r\n\t\r\ndef sortedIntersect(a, b):\r\n\r\n\tif a == None or b == None:\r\n\t\treturn\r\n\r\n\tif a.data &lt; b.data:\r\n\t\treturn sortedIntersect(a.next, b)\r\n\r\n\tif a.data &gt; b.data:\r\n\t\treturn sortedIntersect(a, b.next)\r\n\r\n\ttemp = Node()\r\n\ttemp.data = a.data\r\n\ttemp.next = sortedIntersect(a.next, b.next)\r\n\treturn temp\r\n\r\n\r\ndef push(head_ref, new_data):\r\n\r\n\tnew_node = Node()\r\n\tnew_node.data = new_data;\r\n\tnew_node.next = (head_ref);\r\n\t(head_ref) = new_node;\r\n\treturn head_ref\r\n\r\ndef printList(node):\r\n\twhile (node != None):\r\n\t\tprint(node.data, end=' ')\r\n\t\tnode = node.next;\r\n\t\r\nif __name__=='__main__':\r\n\ta = None;\r\n\tb = None;\r\n\tintersect = None;\r\n\r\n\ta = push(a, 8);\r\n\ta = push(a, 4);\r\n\ta = push(a, 3);\r\n\ta = push(a, 0);\r\n\r\n\tb = push(b, 9);\r\n\tb = push(b, 8);\r\n\tb = push(b, 4);\r\n\r\n\tintersect = sortedIntersect(a, b);\r\n\tprintList(intersect);\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\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_3780 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_3780 a\"),jQuery(\"#tab-content_3780\"));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<pre><code>Output: 4\u21928<\/code><\/pre>\n<p><strong>Time Complexity for intersection of 2 sorted linked list<br \/>\n:<\/strong> O(m+n) where m and n are the numbers of nodes in the first linked list and second linked list respectively.<\/p>\n<p><strong>Space Complexity of intersection of 2 sorted linked list<br \/>\n:<\/strong> O(max(m, n)), as the output list can store at most min(m,n) nodes .<\/p>\n<p><strong>Conclusion<\/strong><\/p>\n<p>In the above article we tried to explain how we can find the intersection of 2 sorted linked list , which is also the common node. We have seen different approaches to traverse through the linked lists and find the intersection node. If you are interested in solving more questions of linked lists then visit the link.<br \/>\n<a href=\"https:\/\/mycode.prepbytes.com\/interview-coding\/practice\/linked-list\">Linked List<\/a>.<\/p>\n<h2>FAQs<\/h2>\n<p><strong>1. What will be the address of the second node of the linked list of which the first node is present?<\/strong><br \/>\nThe address of the second will be the address of the first with addition of the size of the first node. It all depends on the size.<\/p>\n<p><strong>2. Is there a cycle present in the linked list?<\/strong><br \/>\nA cycle can be present in the linked list, imagine if we want to reach any node again, then we can by pointing the next of the last node to the desired node.<\/p>\n<p><strong>3. When does the loop exist in the linked list?<\/strong><br \/>\nGenerally, a loop exists in a linked list when no NULL is reached when we traverse through the link.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We have already seen how union in linked lists work.Now in the insertion there are two linked lists which are given where we have to traverse through those lists to find a common node which is also equal. Then that node is called an intersecting node. Now in this article lets just look into the [&hellip;]<\/p>\n","protected":false},"author":3,"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-3774","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>The intersection of two Sorted Linked Lists | Linked list articles | PrepBytes Blog<\/title>\n<meta name=\"description\" content=\"Given two sorted linked lists, you have to print the intersection of both linked lists in the most efficient way.\" \/>\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\/the-intersection-of-two-sorted-linked-lists\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"The intersection of two Sorted Linked Lists | Linked list articles | PrepBytes Blog\" \/>\n<meta property=\"og:description\" content=\"Given two sorted linked lists, you have to print the intersection of both linked lists in the most efficient way.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/the-intersection-of-two-sorted-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-10T12:30:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-22T07:30:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644926625068-118.The%20intersection%20of%20two%20Sorted%20Linked%20Lists_Artboard%201.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=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/the-intersection-of-two-sorted-linked-lists\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/the-intersection-of-two-sorted-linked-lists\/\"},\"author\":{\"name\":\"PrepBytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e\"},\"headline\":\"The intersection of two Sorted Linked Lists\",\"datePublished\":\"2021-08-10T12:30:02+00:00\",\"dateModified\":\"2022-11-22T07:30:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/the-intersection-of-two-sorted-linked-lists\/\"},\"wordCount\":1040,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/the-intersection-of-two-sorted-linked-lists\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644926625068-118.The%20intersection%20of%20two%20Sorted%20Linked%20Lists_Artboard%201.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/the-intersection-of-two-sorted-linked-lists\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/the-intersection-of-two-sorted-linked-lists\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/the-intersection-of-two-sorted-linked-lists\/\",\"name\":\"The intersection of two Sorted Linked Lists | Linked list articles | PrepBytes Blog\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/the-intersection-of-two-sorted-linked-lists\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/the-intersection-of-two-sorted-linked-lists\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644926625068-118.The%20intersection%20of%20two%20Sorted%20Linked%20Lists_Artboard%201.png\",\"datePublished\":\"2021-08-10T12:30:02+00:00\",\"dateModified\":\"2022-11-22T07:30:44+00:00\",\"description\":\"Given two sorted linked lists, you have to print the intersection of both linked lists in the most efficient way.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/the-intersection-of-two-sorted-linked-lists\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/the-intersection-of-two-sorted-linked-lists\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/the-intersection-of-two-sorted-linked-lists\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644926625068-118.The%20intersection%20of%20two%20Sorted%20Linked%20Lists_Artboard%201.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644926625068-118.The%20intersection%20of%20two%20Sorted%20Linked%20Lists_Artboard%201.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/the-intersection-of-two-sorted-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\":\"The intersection of two Sorted 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\/39fcf072e04987f16796546f2ca83c2e\",\"name\":\"PrepBytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g\",\"caption\":\"PrepBytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/prepbytes\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"The intersection of two Sorted Linked Lists | Linked list articles | PrepBytes Blog","description":"Given two sorted linked lists, you have to print the intersection of both linked lists in the most efficient way.","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\/the-intersection-of-two-sorted-linked-lists\/","og_locale":"en_US","og_type":"article","og_title":"The intersection of two Sorted Linked Lists | Linked list articles | PrepBytes Blog","og_description":"Given two sorted linked lists, you have to print the intersection of both linked lists in the most efficient way.","og_url":"https:\/\/prepbytes.com\/blog\/the-intersection-of-two-sorted-linked-lists\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-08-10T12:30:02+00:00","article_modified_time":"2022-11-22T07:30:44+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644926625068-118.The%20intersection%20of%20two%20Sorted%20Linked%20Lists_Artboard%201.png","type":"","width":"","height":""}],"author":"PrepBytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"PrepBytes","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/the-intersection-of-two-sorted-linked-lists\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/the-intersection-of-two-sorted-linked-lists\/"},"author":{"name":"PrepBytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e"},"headline":"The intersection of two Sorted Linked Lists","datePublished":"2021-08-10T12:30:02+00:00","dateModified":"2022-11-22T07:30:44+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/the-intersection-of-two-sorted-linked-lists\/"},"wordCount":1040,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/the-intersection-of-two-sorted-linked-lists\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644926625068-118.The%20intersection%20of%20two%20Sorted%20Linked%20Lists_Artboard%201.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/the-intersection-of-two-sorted-linked-lists\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/the-intersection-of-two-sorted-linked-lists\/","url":"https:\/\/prepbytes.com\/blog\/the-intersection-of-two-sorted-linked-lists\/","name":"The intersection of two Sorted Linked Lists | Linked list articles | PrepBytes Blog","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/the-intersection-of-two-sorted-linked-lists\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/the-intersection-of-two-sorted-linked-lists\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644926625068-118.The%20intersection%20of%20two%20Sorted%20Linked%20Lists_Artboard%201.png","datePublished":"2021-08-10T12:30:02+00:00","dateModified":"2022-11-22T07:30:44+00:00","description":"Given two sorted linked lists, you have to print the intersection of both linked lists in the most efficient way.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/the-intersection-of-two-sorted-linked-lists\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/the-intersection-of-two-sorted-linked-lists\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/the-intersection-of-two-sorted-linked-lists\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644926625068-118.The%20intersection%20of%20two%20Sorted%20Linked%20Lists_Artboard%201.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644926625068-118.The%20intersection%20of%20two%20Sorted%20Linked%20Lists_Artboard%201.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/the-intersection-of-two-sorted-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":"The intersection of two Sorted 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\/39fcf072e04987f16796546f2ca83c2e","name":"PrepBytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g","caption":"PrepBytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/prepbytes\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/3774","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\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=3774"}],"version-history":[{"count":6,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/3774\/revisions"}],"predecessor-version":[{"id":10679,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/3774\/revisions\/10679"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=3774"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=3774"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=3774"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}