{"id":5856,"date":"2021-10-26T09:29:18","date_gmt":"2021-10-26T09:29:18","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=5856"},"modified":"2022-11-28T12:46:47","modified_gmt":"2022-11-28T12:46:47","slug":"count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value\/","title":{"rendered":"Count pairs from two linked lists whose sum is equal to a given value"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012316328-Article_207.png\" alt=\"\" \/><\/p>\n<p>This blog will discuss an important problem of count pairs from two linked with given sum Such problems do come in interviews.<br \/>\nBefore solving the problem, it\u2019s recommended that a known basic traversal in the Linked List check Linked List.<br \/>\nIn this blog, we dive deep into each detail to get a number of pairs from two linked lists whose sum is equal to a given value.<br \/>\nLet\u2019s look at the problem statement of count pairs from two linked with given sum for a better understanding.<\/p>\n<h2>How to count pairs from two linked list with given sum<\/h2>\n<p>We will be given two linked lists and an integer target as input, and we need to count pairs of integers in both lists such that each element of the pair belongs to different lists and their sum is equal to the given target integer.<\/p>\n<p>To understand the problem statement, let\u2019s take an example.<\/p>\n<p>If the linked lists given to us are 1\u21923\u21925\u21928\u2192NULL, 6\u21924\u21921\u2192NULL, and the target = 9. Then, according to the problem statement:<\/p>\n<ul>\n<li>We need to find pairs of integers such that each element of the pair belongs to a different list, and their sum is equal to 9.<\/li>\n<li>All such pairs are &#8211; (3,6), (8,1) and (5,4) <\/li>\n<li>There are three possible pairs, so; we need to print 3 as output, i.e., the total count of possible pairs.<\/li>\n<\/ul>\n<p>At this point, we have understood the problem statement. Now we will try to formulate an approach for this problem.<\/p>\n<p>Before moving to the approach section, try to think about how you can approach this problem.<\/p>\n<ul>\n<li>If stuck, no problem, we will thoroughly see how we can approach this problem in the next section.<\/li>\n<\/ul>\n<p>Let\u2019s move to the approach section.<\/p>\n<h2>Approach 1 of count pairs from two linked with given sum<\/h2>\n<p>The naive approach that comes to our mind is that first, select a node in the first list and for each selected node in the first list, iterate in the second list and check if the sum of node\u2019s (selected node from first list + node from the second list) is equal to the target or not.<\/p>\n<p><strong>Time Complexity of count pairs from two linked with given sum:<\/strong> O(N<sup>2<\/sup>), N is the number of nodes in a list.<br \/>\n<strong>Space Complexity of count pairs from two linked with given sum:<\/strong> O(1)<\/p>\n<p>The above approach gives the correct result, but its time complexity is high. Can we reduce the time complexity? Let us find out If we can do so in the below approach.<\/p>\n<h2>Approach 2 of count pairs from two linked with given sum<\/h2>\n<ul>\n<li>In this approach, we store all elements of the first list in a HashSet.<\/li>\n<li>Then, we iterate on the elements of the second list and find if the difference in the value of the current element of the second list and target exists in hash or not.<\/li>\n<li>If it exists in the HashSet, then we will increase the count by one.<\/li>\n<\/ul>\n<h2>Algorithm of count pairs from two linked with given sum<\/h2>\n<ol>\n<li>Initialize a <strong>result<\/strong> variable with 0 and create an unordered set.<\/li>\n<li>Push all elements of the first list in the set created above.<\/li>\n<li>\n<p>Run a while loop till <strong>head2<\/strong> (pointer to the second list) is not NULL.<\/p>\n<ul>\n<li>\n<p>Check if <strong>(target &#8211; head2-&gt;data)<\/strong> exists in the set or not<\/p>\n<ul>\n<li>If it exists then, increment the <strong>result<\/strong> by one.<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>Move the pointer <strong>head2<\/strong> forward by one node.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>After the loop is executed, return <strong>result<\/strong> from the function.<\/li>\n<\/ol>\n<h2>Code Implementation of count pairs from two linked with given sum<\/h2>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_5868 {\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_5868 .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_5868 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_5868 .wpsm_nav-tabs > li.active > a, #tab_container_5868 .wpsm_nav-tabs > li.active > a:hover, #tab_container_5868 .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_5868 .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_5868 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_5868 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_5868 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_5868 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_5868 .wpsm_nav-tabs > li > a:hover , #tab_container_5868 .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_5868 .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_5868 .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_5868 .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_5868 .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_5868 .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_5868 .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_5868 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_5868 .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_5868 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_5868 .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_5868 .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_5868\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_5868\">\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_5868_1\" aria-controls=\"tabs_desc_5868_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_5868_2\" aria-controls=\"tabs_desc_5868_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_5868_3\" aria-controls=\"tabs_desc_5868_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_5868_4\" aria-controls=\"tabs_desc_5868_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_5868\">\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_5868_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\nstruct Node\r\n{\r\n  int data;\r\n  struct Node* next;\r\n};\r\n \r\n\/\/ function to insert a node at the\r\n\/\/ 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(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 to 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 count all pairs from both the linked\r\n\/\/ lists whose sum is equal to a given value\r\nint countPairs(struct Node* head1, struct Node* head2,\r\n                                              int x)\r\n{\r\n    int count = 0;\r\n     \r\n    \/\/ sort head1 in ascending order and\r\n    \/\/ head2 in descending order\r\n    \/\/ sort (head1), sort (head2)\r\n    \/\/ For simplicity both lists are considered to be\r\n    \/\/ sorted in the respective orders\r\n     \r\n    \/\/ traverse both the lists from left to right\r\n    while (head1 != NULL &amp;&amp; head2 != NULL)\r\n    {\r\n        \/\/ if this sum is equal to 'x', then move both\r\n        \/\/ the lists to next nodes and increment 'count'\r\n        if ((head1-&gt;data + head2-&gt;data) == x)\r\n        {\r\n            head1 = head1-&gt;next;\r\n            head2 = head2-&gt;next;\r\n            count++;   \r\n        }   \r\n         \r\n        \/\/ if this sum is greater than x, then\r\n        \/\/ move head2 to next node\r\n        else if ((head1-&gt;data + head2-&gt;data) &gt; x)\r\n            head2 = head2-&gt;next;\r\n             \r\n        \/\/ else move head1 to next node   \r\n        else\r\n            head1 = head1-&gt;next;\r\n    }       \r\n         \r\n    \/\/ required count of pairs    \r\n    return count;\r\n}\r\n \r\n\/\/ Driver program to test above\r\nint main()\r\n{\r\n    struct Node* head1 = NULL;\r\n    struct Node* head2 = NULL;\r\n     \r\n    \/\/ create linked list1 1-&gt;3-&gt;5-&gt;7\r\n    \/\/ assumed to be in ascending order\r\n    push(&amp;head1, 7);\r\n    push(&amp;head1, 5);\r\n    push(&amp;head1, 3);\r\n    push(&amp;head1, 1);   \r\n     \r\n    \/\/ create linked list2 8-&gt;5-&gt;3-&gt;2\r\n    \/\/ assumed to be in descending order\r\n    push(&amp;head2, 2);\r\n    push(&amp;head2, 3);\r\n    push(&amp;head2, 5);\r\n    push(&amp;head2, 8);\r\n     \r\n    int x = 10;\r\n    int ans = countPairs(head1, head2, x);\r\n    printf(&quot;Count = %d&quot;,ans);\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_5868_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\nclass Node{\r\n    public:\r\n    int data;\r\n    Node* next;\r\n    Node(int x){\r\n        data = x;\r\n        next = NULL;\r\n    }\r\n};\r\n\r\nint countPairs(Node* head1, Node* head2,int target)\r\n{\r\n    int result = 0;\r\n     \r\n    unordered_set&lt;int&gt; hash_set;\r\n     \r\n    \/\/insert all elements of first list in\r\n    \/\/the hash set \r\n    while (head1 != NULL)\r\n    {\r\n        hash_set.insert(head1-&gt;data);      \r\n        head1 = head1-&gt;next;\r\n    }\r\n     \r\n    \/\/ iterate on each elements of second list\r\n    while (head2 != NULL)   \r\n    {\r\n        \/\/find if (target - currentData) exists\r\n        \/\/in the hashset\r\n        if (hash_set.find(target - head2-&gt;data) != hash_set.end())\r\n            result++;\r\n         \r\n        head2 = head2-&gt;next;   \r\n    }\r\n    \/\/ return the result    \r\n    return result;\r\n}\r\nint main(void){\r\n    Node* first = NULL;\r\n    first = new Node(1);\r\n    first-&gt;next = new Node(3);\r\n    first-&gt;next-&gt;next = new Node(5);\r\n    first-&gt;next-&gt;next-&gt;next = new Node(8);\r\n\r\n    Node* second = NULL;\r\n    second = new Node(6);\r\n    second-&gt;next = new Node(4);\r\n    second-&gt;next-&gt;next = new Node(1);\r\n    cout&lt;&lt;&quot;Count of pairs having sum &quot;&lt;&lt;9&lt;&lt;&quot; are: &quot;;\r\n    cout&lt;&lt;countPairs(first,second,9);\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_5868_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 Node \r\n{\r\n    int data;\r\n    Node next;\r\n    Node(int data)\r\n    {\r\n        this.data=data;\r\n    }\r\n}\r\n\r\npublic class CountParis \r\n{\r\n    static int countPairs(Node first,Node second, int target)\r\n    {\r\n        int result = 0;\r\n    \r\n    \/\/ iterate both lists\r\n    while (first != null &amp;&amp; second != null)\r\n    {\r\n        \/\/calculate 'sum' \r\n        int sum = first.data + second.data;\r\n        \/\/if sum equals target, increase result by 1\r\n        \/\/and move both pointers\r\n        if (sum == target)\r\n        {\r\n            first = first.next;\r\n            second = second.next;\r\n            result++;    \r\n        }    \r\n        \r\n        \/\/ if sum is more than target, move \r\n        \/\/ second pointer\r\n        else if (sum &gt; target)\r\n            second = second.next;\r\n            \r\n        \/\/ else move first pointer    \r\n        else\r\n            first = first.next;\r\n    }        \r\n        \r\n    \/\/ required count of pairs     \r\n    return result;\r\n    }\r\n    public static void main(String[] args) \r\n    {\r\n        Node first=null;\r\n        first=new Node(1);\r\n        first.next=new Node(3);\r\n        first.next.next=new Node(5);\r\n        first.next.next.next=new Node(8);\r\n        \r\n        \r\n        Node second=new Node(6);\r\n        second.next=new Node(4);\r\n        second.next.next=new Node(1);\r\n\r\n        System.out.println(&quot;Count of pairs having sum 9 are:&quot;);\r\n        System.out.println(countPairs(first, second, 9));\r\n    }\r\n}\r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\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_5868_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\t\r\n\tdef __init__(self):\r\n\t\tself.data = 0\r\n\t\tself.next = None\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 countPairs(first, second, target):\r\n\tresult = 0\r\n\r\n\twhile first and second:\r\n\r\n\t\tsum = first.data + second.data\r\n\r\n\t\tif sum == target:\r\n\t\t\tfirst = first.next\r\n\t\t\tsecond = second.next \r\n\t\t\tresult += 1\r\n\r\n\t\telif sum &gt; target:\r\n\t\t\tsecond = second.next\r\n\r\n\t\telse:\r\n\t\t\tfirst = first.next\r\n\r\n\treturn result\r\n\r\nif __name__=='__main__':\r\n\t\r\n\thead1 = None\r\n\thead2 = None\r\n\t\r\n\thead1 = push(head1, 1)\r\n\thead1 = push(head1, 3)\r\n\thead1 = push(head1, 5)\r\n\thead1 = push(head1, 8)\r\n\t\r\n\thead2 = push(head2, 6)\r\n\thead2 = push(head2, 4)\r\n\thead2 = push(head2, 1)\r\n\thead2 = push(head2, 9)\r\n\t\r\n\tx = 9\r\n\t\r\n\tprint(&quot;Count of pairs having sum&quot;, x,&quot;are:&quot;, countPairs(head1, head2, x))\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_5868 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_5868 a\"),jQuery(\"#tab-content_5868\"));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\nCount of pairs having sum 9 are: 3<\/code><\/pre>\n<p><strong>Time Complexity of count pairs from two linked with given sum:<\/strong> O(n), n is the number of nodes in a list.<\/p>\n<p><strong>Space Complexity of count pairs from two linked with given sum:<\/strong> O(n), n is the number of nodes in a list.<\/p>\n<p>In the above approach, we are using extra space to store the elements in a hash set. Can we reduce the extra space used? Let us find out If we can do so in the below approach.<\/p>\n<h2>Approach 3 of count pairs with given sum<\/h2>\n<ul>\n<li>At first, we need to <strong>sort the first list in ascending order and the second list in descending order<\/strong>.<\/li>\n<li>The above step is performed so that we can predict whether moving forward in the lists is going to increase our current sum or decrease it.<\/li>\n<li>It is evident that moving forward in the first list is going to increase the pair sum, and moving forward in the second one is going to decrease it.<\/li>\n<li>Now, we will initialize two different pointers at the head of both lists.<\/li>\n<li>We will compute the sum pointed by the pointers in each step, and If the <strong>sum is equal to the target<\/strong>, we will increase our result by one and move both pointers simultaneously.<\/li>\n<li>If the <strong>sum is less than the target<\/strong>, we need to increase it so we will move the pointer of the first list forward by one node.<\/li>\n<li>Else, we will move the pointer of the second list by one node.<\/li>\n<\/ul>\n<h2>Algorithm count pairs with given sum<\/h2>\n<ol>\n<li>Sort the <strong>first list in ascending order and the second list in descending order<\/strong>.<\/li>\n<li>Initialize a variable <strong>result<\/strong> by 0.<\/li>\n<li>Run a while loop till any one of <strong>first<\/strong> or <strong>second<\/strong> becomes NULL.<\/li>\n<li>Calculate <strong>sum<\/strong> as the sum of data of nodes pointed by <strong>first<\/strong> and <strong>second<\/strong> pointers.<\/li>\n<li>If the <strong>sum<\/strong> is equal to the <strong>target<\/strong>, increase the <strong>result<\/strong> by one and move both pointers forward by one node.<\/li>\n<li>If the <strong>sum<\/strong> is greater than the <strong>target<\/strong>, move the <strong>second<\/strong> pointer forward by one node.<\/li>\n<li>Else, move the <strong>first<\/strong> pointer forward by one node.<\/li>\n<\/ol>\n<p><strong>Note:<\/strong> For simplicity, in dry run and code implementation, we took the first and the second linked lists, which are already sorted in ascending and descending order, respectively. If you want to know how to sort a linked list, please feel free to refer to this <a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/c-program-for-merge-sort-for-linked-lists\/\">article<\/a> for sorting related concepts of linked list. Also, inside the code implementation, we haven&#8217;t provided the sorting function (function to sort the linked list); if you want to take unsorted linked lists, please sort them in proper format (the first list will be sorted in ascending order and the second list in descending order) using code from the above-mentioned article, before passing them to <strong>countPairs()<\/strong> function in the code.<\/p>\n<p>Also, note that to sort a linked list in descending order, you can first sort the linked list in ascending order, and then you can reverse the sorted linked list. <\/p>\n<h3>Dry Run of count pairs from two linked with given sum<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/10\/p_1-6.jpg\" alt=\"\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/10\/p_2_3.jpg\" alt=\"\" \/><\/p>\n<h2>Code Implementation of count pairs from two linked with given sum<\/h2>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_5867 {\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_5867 .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_5867 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_5867 .wpsm_nav-tabs > li.active > a, #tab_container_5867 .wpsm_nav-tabs > li.active > a:hover, #tab_container_5867 .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_5867 .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_5867 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_5867 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_5867 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_5867 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_5867 .wpsm_nav-tabs > li > a:hover , #tab_container_5867 .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_5867 .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_5867 .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_5867 .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_5867 .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_5867 .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_5867 .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_5867 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_5867 .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_5867 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_5867 .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_5867 .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_5867\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_5867\">\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_5867_1\" aria-controls=\"tabs_desc_5867_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_5867_2\" aria-controls=\"tabs_desc_5867_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_5867_3\" aria-controls=\"tabs_desc_5867_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_5867_4\" aria-controls=\"tabs_desc_5867_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_5867\">\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_5867_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\r\n#include<stdio.h>\r\n#include<stdlib.h>\r\n#include<stdbool.h>\r\n\r\nstruct Node {\r\n    int data;\r\n    struct Node* next;\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(\"%d \", node->data);\r\n        node = node->next;\r\n    }\r\n}\r\n \r\n\/\/ The main function that\r\n\/\/ takes an array of lists\r\n\/\/ arr[0..last] and generates\r\n\/\/ the sorted output\r\nstruct Node* mergeKLists(struct Node* arr[], int last)\r\n{\r\n \r\n    \/\/ Traverse form second list to last\r\n    for (int i = 1; i <= last; i++) {\r\n        while (true) {\r\n            \/\/ head of both the lists,\r\n            \/\/ 0 and ith list.\r\n            struct Node *head_0 = arr[0], *head_i = arr[i];\r\n \r\n            \/\/ Break if list ended\r\n            if (head_i == NULL)\r\n                break;\r\n \r\n            \/\/ Smaller than first element\r\n            if (head_0->data >= head_i->data) {\r\n                arr[i] = head_i->next;\r\n                head_i->next = head_0;\r\n                arr[0] = head_i;\r\n            }\r\n            else\r\n                \/\/ Traverse the first list\r\n                while (head_0->next != NULL) {\r\n                    \/\/ Smaller than next element\r\n                    if (head_0->next->data\r\n                        >= head_i->data) {\r\n                        arr[i] = head_i->next;\r\n                        head_i->next = head_0->next;\r\n                        head_0->next = head_i;\r\n                        break;\r\n                    }\r\n                    \/\/ go to next node\r\n                    head_0 = head_0->next;\r\n \r\n                    \/\/ if last node\r\n                    if (head_0->next == NULL) {\r\n                        arr[i] = head_i->next;\r\n                        head_i->next = NULL;\r\n                        head_0->next = head_i;\r\n                        head_0->next->next = NULL;\r\n                        break;\r\n                    }\r\n                }\r\n        }\r\n    }\r\n \r\n    return arr[0];\r\n}\r\n \r\n\/\/ Utility function to create a new node.\r\nstruct Node* newNode(int data)\r\n{\r\n    struct Node *temp = (struct Node *) malloc(sizeof(struct Node));\r\n    temp->data = data;\r\n    temp->next = NULL;\r\n    return temp;\r\n}\r\n \r\n\/\/ Driver program to test\r\n\/\/ above functions\r\nint main()\r\n{\r\n    \/\/ Number of linked lists\r\n    int k = 3;\r\n \r\n    \/\/ Number of elements in each list\r\n    int n = 4;\r\n \r\n    \/\/ an array of pointers storing the\r\n    \/\/ head nodes of the linked lists\r\n    struct Node* arr[k];\r\n \r\n    arr[0] = newNode(10);\r\n    arr[0]->next = newNode(30);\r\n    arr[0]->next->next = newNode(50);\r\n    arr[0]->next->next->next = newNode(70);\r\n \r\n    arr[1] = newNode(20);\r\n    arr[1]->next = newNode(40);\r\n    arr[1]->next->next = newNode(60);\r\n    arr[1]->next->next->next = newNode(80);\r\n \r\n    arr[2] = newNode(0);\r\n    arr[2]->next = newNode(90);\r\n    arr[2]->next->next = newNode(100);\r\n    arr[2]->next->next->next = newNode(11);\r\n \r\n    \/\/ Merge all lists\r\n    struct Node* head = mergeKLists(arr, k - 1);\r\n \r\n    printList(head);\r\n \r\n    return 0;\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_5867_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\nclass Node{\r\n    public:\r\n    int data;\r\n    Node* next;\r\n    Node(int x){\r\n        data = x;\r\n        next = NULL;\r\n    }\r\n};\r\n\r\nint countPairs(Node* first, Node* second,int target)\r\n{\r\n    int result = 0;\r\n    \r\n    \/\/ iterate both lists\r\n    while (first != NULL &amp;&amp; second != NULL)\r\n    {\r\n        \/\/calculate 'sum' \r\n        int sum = first-&gt;data + second-&gt;data;\r\n\r\n        \/\/if sum equals target, increase result by 1\r\n        \/\/and move both pointers\r\n        if (sum == target)\r\n        {\r\n            first = first-&gt;next;\r\n            second = second-&gt;next;\r\n            result++;    \r\n        }    \r\n        \r\n        \/\/ if sum is more than target, move \r\n        \/\/ second pointer\r\n        else if (sum &gt; target)\r\n            second = second-&gt;next;\r\n            \r\n        \/\/ else move first pointer    \r\n        else\r\n            first = first-&gt;next;\r\n    }        \r\n        \r\n    \/\/ required count of pairs     \r\n    return result;\r\n}\r\n\r\nint main(void){\r\n    Node* first = NULL;\r\n    first = new Node(1);\r\n    first-&gt;next = new Node(3);\r\n    first-&gt;next-&gt;next = new Node(5);\r\n    first-&gt;next-&gt;next-&gt;next = new Node(8);\r\n\r\n    Node* second = NULL;\r\n    second = new Node(6);\r\n    second-&gt;next = new Node(4);\r\n    second-&gt;next-&gt;next = new Node(1);\r\n    cout&lt;&lt;&quot;Count of pairs having sum &quot;&lt;&lt;9&lt;&lt;&quot; are: &quot;;\r\n    cout&lt;&lt;countPairs(first,second,9);\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_5867_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\nclass Node \r\n{\r\n    int data;\r\n    Node next;\r\n    Node(int data)\r\n    {\r\n        this.data=data;\r\n    }\r\n}\r\n\r\npublic class CountParis \r\n{\r\n    static int countPairs(Node head1,Node head2, int target)\r\n    {\r\n        int res=0;\r\n        HashSet&lt;Integer&gt; h=new HashSet&lt;&gt;();\r\n\r\n        while(head1!=null)\r\n        {\r\n            h.add(head1.data);\r\n            head1=head1.next;\r\n        }\r\n        while(head2!=null)\r\n        {\r\n            if(h.contains(target-head2.data))\r\n            {\r\n                res++;\r\n            }\r\n            head2=head2.next;\r\n        }\r\n        return res;\r\n    }\r\n    public static void main(String[] args) \r\n    {\r\n        Node first=null;\r\n        first=new Node(1);\r\n        first.next=new Node(3);\r\n        first.next.next=new Node(5);\r\n        first.next.next.next=new Node(8);\r\n        \r\n        \r\n        Node second=new Node(6);\r\n        second.next=new Node(4);\r\n        second.next.next=new Node(1);\r\n\r\n        System.out.println(&quot;Count of pairs having sum 9 are:&quot;);\r\n        System.out.println(countPairs(first, second, 9));\r\n    }\r\n}\r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\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_5867_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\t\r\n\tdef __init__(self):\r\n\t\tself.data = 0\r\n\t\tself.next = None\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 countPairs(head1, head2, x):\r\n\tcount = 0\r\n\tus = set()\r\n\t\r\n\t# add all the elements of 1st list\r\n\t# in the hash table(unordered_set 'us')\r\n\twhile (head1 != None):\r\n\t\tus.add(head1.data)\r\n\t\t\r\n\t\t# move to next node\r\n\t\thead1 = head1.next\r\n\t\r\n\t# iterate on each element of 2nd list\r\n\twhile (head2 != None):\r\n\t\r\n\t\t# find (x - head2.data) in 'us'\r\n\t\tif ((x - head2.data) in us):\r\n\t\t\tcount += 1\r\n\t\t\r\n\t\t# move to next node\r\n\t\thead2 = head2.next\r\n\r\n\t# return the count\t\r\n\treturn count\r\n\r\nif __name__=='__main__':\r\n\t\r\n\thead1 = None\r\n\thead2 = None\r\n\t\r\n\thead1 = push(head1, 1)\r\n\thead1 = push(head1, 3)\r\n\thead1 = push(head1, 5)\r\n\thead1 = push(head1, 8)\r\n\t\r\n\thead2 = push(head2, 6)\r\n\thead2 = push(head2, 4)\r\n\thead2 = push(head2, 1)\r\n\thead2 = push(head2, 9)\r\n\t\r\n\tx = 9\r\n\t\r\n\tprint(&quot;Count of pairs having sum&quot;, x,&quot;are:&quot;, countPairs(head1, head2, x))\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_5867 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_5867 a\"),jQuery(\"#tab-content_5867\"));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\nCount of pairs having sum 9 are: 3<\/code><\/pre>\n<p><strong>Time Complexity of count pairs from two linked with given sum:<\/strong> O(N log N), N is the number of nodes in the list<\/p>\n<p><strong>Space Complexity of count pairs from two linked with given sum:<\/strong> O(1)<\/p>\n<p><strong>Conclusion<\/strong><\/p>\n<p>So, this article taught us how to solve the problem of data structure, i.e., count pairs from two linked lists whose sum is equal to a given value. We also saw how to approach the problem using a naive approach followed by two efficient solutions. We discussed an iterative implementation and some basic traversal in linked lists using examples, pseudocode, and code in detail. If you want to solve more questions on Linked List, which is curated by our expert mentors at PrepBytes, you can follow this link <a href=\"https:\/\/mycode.prepbytes.com\/interview-coding\/practice\/linked-list\">Linked List<\/a>.<\/p>\n<h2>FAQs related to count pairs from two linked with given sum<\/h2>\n<p><strong>1. What are the advantages of a linked list?<\/strong><br \/>\nSome advantages of the linked list are as follows:<\/p>\n<ul>\n<li>Insertion and deletion operations can be implemented very easily, and these are not costly as they do not require the shifting of elements.<\/li>\n<li>They are dynamic. Hence, we can change their size whenever required.<\/li>\n<li>Stacks and queues can be implemented very easily using Linked Lists.<\/li>\n<\/ul>\n<p><strong>2. How does merge sort for linked list work?<\/strong><br \/>\nThe merge sort algorithm is a divide and conquers algorithm it divides the list into smaller sublists until each sublist contains only a single element, and a list of size one is always sorted; using this property, it merges two sorted sublists into a single sorted list.<\/p>\n<p><strong>3. Can we use two pointer approach without sorting the lists?<\/strong><br \/>\nNo, Because in the unsorted list we can\u2019t decide whether its next element is greater or less than the current element. So we don\u2019t move our pointer to increase or decrease the sum. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>This blog will discuss an important problem of count pairs from two linked with given sum Such problems do come in interviews. Before solving the problem, it\u2019s recommended that a known basic traversal in the Linked List check Linked List. In this blog, we dive deep into each detail to get a number of pairs [&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-5856","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>Count pairs from two linked lists whose sum is equal to a given value | Linked List | Prepbytes<\/title>\n<meta name=\"description\" content=\"Learn how to count pairs from two linked lists whose sum is equal to a given value.\" \/>\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\/count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Count pairs from two linked lists whose sum is equal to a given value | Linked List | Prepbytes\" \/>\n<meta property=\"og:description\" content=\"Learn how to count pairs from two linked lists whose sum is equal to a given value.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value\/\" \/>\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-10-26T09:29:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-28T12:46:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012316328-Article_207.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=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Count pairs from two linked lists whose sum is equal to a given value\",\"datePublished\":\"2021-10-26T09:29:18+00:00\",\"dateModified\":\"2022-11-28T12:46:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value\/\"},\"wordCount\":1424,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012316328-Article_207.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value\/\",\"name\":\"Count pairs from two linked lists whose sum is equal to a given value | Linked List | Prepbytes\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012316328-Article_207.png\",\"datePublished\":\"2021-10-26T09:29:18+00:00\",\"dateModified\":\"2022-11-28T12:46:47+00:00\",\"description\":\"Learn how to count pairs from two linked lists whose sum is equal to a given value.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012316328-Article_207.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012316328-Article_207.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value\/#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\":\"Count pairs from two linked lists whose sum is equal to a given value\"}]},{\"@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":"Count pairs from two linked lists whose sum is equal to a given value | Linked List | Prepbytes","description":"Learn how to count pairs from two linked lists whose sum is equal to a given value.","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\/count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value\/","og_locale":"en_US","og_type":"article","og_title":"Count pairs from two linked lists whose sum is equal to a given value | Linked List | Prepbytes","og_description":"Learn how to count pairs from two linked lists whose sum is equal to a given value.","og_url":"https:\/\/prepbytes.com\/blog\/count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-10-26T09:29:18+00:00","article_modified_time":"2022-11-28T12:46:47+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012316328-Article_207.png","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Count pairs from two linked lists whose sum is equal to a given value","datePublished":"2021-10-26T09:29:18+00:00","dateModified":"2022-11-28T12:46:47+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value\/"},"wordCount":1424,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012316328-Article_207.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value\/","url":"https:\/\/prepbytes.com\/blog\/count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value\/","name":"Count pairs from two linked lists whose sum is equal to a given value | Linked List | Prepbytes","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012316328-Article_207.png","datePublished":"2021-10-26T09:29:18+00:00","dateModified":"2022-11-28T12:46:47+00:00","description":"Learn how to count pairs from two linked lists whose sum is equal to a given value.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012316328-Article_207.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012316328-Article_207.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/count-pairs-from-two-linked-lists-whose-sum-is-equal-to-a-given-value\/#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":"Count pairs from two linked lists whose sum is equal to a given value"}]},{"@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\/5856","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=5856"}],"version-history":[{"count":7,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/5856\/revisions"}],"predecessor-version":[{"id":10802,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/5856\/revisions\/10802"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=5856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=5856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=5856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}