{"id":684,"date":"2025-07-18T14:20:47","date_gmt":"2025-07-18T08:50:47","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=684"},"modified":"2025-07-18T14:20:48","modified_gmt":"2025-07-18T08:50:48","slug":"find-pairs-with-given-sum-in-doubly-linked-list","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/find-pairs-with-given-sum-in-doubly-linked-list\/","title":{"rendered":"Find pairs with given sum in doubly linked list | GFG Practice | Optimal Solution"},"content":{"rendered":"\n<p>Given a sorted doubly linked list and a target sum, find all pairs of nodes whose sum equals the target.\u00a0Return the pairs as a list of lists, where each inner list contains two elements representing a valid pair.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/www.geeksforgeeks.org\/problems\/find-pairs-with-given-sum-in-doubly-linked-list\/1\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:  <\/strong>\n1 &lt;-&gt; 2 &lt;-&gt; 4 &lt;-&gt; 5 &lt;-&gt; 6 &lt;-&gt; 8 &lt;-&gt; 9\ntarget = 7\n<strong>Output: <\/strong>(1, 6), (2,5)\n<strong>Explanation: <\/strong>We can see that there are two pairs \n(1, 6) and (2,5) with sum 7.<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input: <\/strong><br>1 &lt;-> 5 &lt;-> 6<br>target = 6<br><strong>Output: <\/strong>(1,5)<br><strong>Explanation: <\/strong>We can see that there is one pairs  (1, 5) with sum 6.<\/pre>\n\n\n\n<p><strong>Your Task:<\/strong><br>You don&#8217;t need to read input or print anything. Your task is to complete the function&nbsp;<strong>findPairsWithGivenSum()&nbsp;<\/strong>which takes head node of the doubly linked list and an integer target&nbsp;as input parameter and returns an array of pairs. If there is no such pair&nbsp;return&nbsp;empty array.<\/p>\n\n\n\n<p><strong>Expected Time Complexity:<\/strong>&nbsp;O(N)<br><strong>Expected Auxiliary Space:<\/strong>&nbsp;O(1)<br><strong>Constraints:<\/strong><br>1 &lt;= N&nbsp;&lt;= 10<sup>5<\/sup><br>1 &lt;= target&nbsp;&lt;= 10<sup>5<\/sup><\/p>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents ub_table-of-contents-collapsed\" id=\"ub_table-of-contents-bf37ed6a-caa6-4c96-943b-83728046e839\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"true\" data-initiallyshow=\"false\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Contents:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">show<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column ub-hide\">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-pairs-with-given-sum-in-doubly-linked-list\/#0-solution-optimal-approach-two-pointers\" style=\"\">Solution: Optimal Approach (Two Pointers)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-pairs-with-given-sum-in-doubly-linked-list\/#1-intuition\" style=\"\">Intuition<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-pairs-with-given-sum-in-doubly-linked-list\/#2-detailed-approach\" style=\"\">Detailed Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-pairs-with-given-sum-in-doubly-linked-list\/#3-code\" style=\"\">Code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-pairs-with-given-sum-in-doubly-linked-list\/#4-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-pairs-with-given-sum-in-doubly-linked-list\/#5-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-pairs-with-given-sum-in-doubly-linked-list\/#6-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-pairs-with-given-sum-in-doubly-linked-list\/#7-simplifying-it\" style=\"\">Simplifying It<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-pairs-with-given-sum-in-doubly-linked-list\/#8-key-insights\" style=\"\">Key Insights<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-pairs-with-given-sum-in-doubly-linked-list\/#9-summary\" style=\"\">Summary<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"0-solution-optimal-approach-two-pointers\">Solution: Optimal Approach (Two Pointers)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-intuition\">Intuition<\/h3>\n\n\n\n<p>Think of this like finding dance partners at a party where everyone is lined up by height! You want pairs whose combined height equals a specific number. The smartest approach is to have one person start from the shortest end and another from the tallest end. If their combined height is too small, the shorter person moves to someone taller. If it&#8217;s too big, the taller person moves to someone shorter. If it&#8217;s perfect, you found a pair! This is exactly what we do with two pointers in a sorted doubly linked list.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-detailed-approach\">Detailed Approach<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Set Up Two Pointers<\/strong>: Start with left pointer at the head (smallest element) and right pointer at the tail (largest element)<\/li>\n\n\n\n<li><strong>Find the Tail<\/strong>: Move the right pointer to the end of the list to start from the largest element<\/li>\n\n\n\n<li><strong>Two Pointer Technique<\/strong>: While left pointer is before right pointer, check their sum<\/li>\n\n\n\n<li><strong>Found a Pair<\/strong>: If sum equals target, add the pair to results and move both pointers inward<\/li>\n\n\n\n<li><strong>Sum Too Large<\/strong>: If sum is greater than target, move right pointer backward (to smaller element)<\/li>\n\n\n\n<li><strong>Sum Too Small<\/strong>: If sum is less than target, move left pointer forward (to larger element)<\/li>\n\n\n\n<li><strong>Continue Until Pointers Meet<\/strong>: Stop when pointers meet or cross each other<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-code\">Code<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def findPairsWithGivenSum(\n        self, target: int, head: Optional[&quot;Node&quot;]\n    ) -&gt; List[List[int]]:\n        left = head          # Left pointer starts at head (smallest element)\n        right = head         # Right pointer will move to tail (largest element)\n        ans = []            # List to store all valid pairs\n        \n        # Move right pointer to the end of the list\n        while right.next:\n            right = right.next\n        \n        # Use two pointers to find pairs\n        while left is not None and right is not None and left.data &lt; right.data:\n            total = left.data + right.data  # Calculate sum of current pair\n            \n            if total == target:\n                # Found a valid pair, add to results\n                ans.append([left.data, right.data])\n                left = left.next     # Move left pointer forward\n                right = right.prev   # Move right pointer backward\n            elif total &gt; target:\n                # Sum is too large, move right pointer to smaller element\n                right = right.prev\n            else:\n                # Sum is too small, move left pointer to larger element\n                left = left.next\n        \n        return ans\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">findPairsWithGivenSum<\/span><span style=\"color: #D4D4D4\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">target<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">head<\/span><span style=\"color: #D4D4D4\">: Optional[<\/span><span style=\"color: #CE9178\">&quot;Node&quot;<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    ) -&gt; List[List[<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">]]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        left = head          <\/span><span style=\"color: #6A9955\"># Left pointer starts at head (smallest element)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        right = head         <\/span><span style=\"color: #6A9955\"># Right pointer will move to tail (largest element)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        ans = []            <\/span><span style=\"color: #6A9955\"># List to store all valid pairs<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Move right pointer to the end of the list<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> right.next:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            right = right.next<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Use two pointers to find pairs<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> left <\/span><span style=\"color: #569CD6\">is<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">not<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">and<\/span><span style=\"color: #D4D4D4\"> right <\/span><span style=\"color: #569CD6\">is<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">not<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">and<\/span><span style=\"color: #D4D4D4\"> left.data &lt; right.data:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            total = left.data + right.data  <\/span><span style=\"color: #6A9955\"># Calculate sum of current pair<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> total == target:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #6A9955\"># Found a valid pair, add to results<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                ans.append([left.data, right.data])<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                left = left.next     <\/span><span style=\"color: #6A9955\"># Move left pointer forward<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                right = right.prev   <\/span><span style=\"color: #6A9955\"># Move right pointer backward<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">elif<\/span><span style=\"color: #D4D4D4\"> total &gt; target:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #6A9955\"># Sum is too large, move right pointer to smaller element<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                right = right.prev<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #6A9955\"># Sum is too small, move left pointer to larger element<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                left = left.next<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> ans<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-code-explanation\">Code Explanation<\/h3>\n\n\n\n<p>This solution uses the classic two-pointer technique optimized for doubly linked lists. We start by positioning the right pointer at the tail of the list. Then we use two pointers moving towards each other from opposite ends. The key insight is that since the list is sorted, we can make intelligent decisions about which pointer to move based on whether the current sum is too large or too small. When the sum equals the target, we found a valid pair and move both pointers inward to look for more pairs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-dry-run\">Dry Run<\/h3>\n\n\n\n<p>Let&#8217;s trace through:&nbsp;<code>1 &lt;-&gt; 2 &lt;-&gt; 4 &lt;-&gt; 5 &lt;-&gt; 6 &lt;-&gt; 8 &lt;-&gt; 9<\/code>, target = 7<\/p>\n\n\n\n<p><strong>Initial Setup:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>left = 1, right = 9, ans = []<\/li>\n<\/ul>\n\n\n\n<p><strong>Step 1:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>total = 1 + 9 = 10<\/li>\n\n\n\n<li>10 > 7, so move right pointer backward: right = 8<\/li>\n<\/ul>\n\n\n\n<p><strong>Step 2:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>total = 1 + 8 = 9<\/li>\n\n\n\n<li>9 > 7, so move right pointer backward: right = 6<\/li>\n<\/ul>\n\n\n\n<p><strong>Step 3:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>total = 1 + 6 = 7<\/li>\n\n\n\n<li>7 == 7, found pair! ans = [<a href=\"https:\/\/www.geeksforgeeks.org\/problems\/find-pairs-with-given-sum-in-doubly-linked-list\/1\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>]<\/li>\n\n\n\n<li>Move both pointers: left = 2, right = 5<\/li>\n<\/ul>\n\n\n\n<p><strong>Step 4:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>total = 2 + 5 = 7<\/li>\n\n\n\n<li>7 == 7, found pair! ans = [<a href=\"https:\/\/www.geeksforgeeks.org\/problems\/find-pairs-with-given-sum-in-doubly-linked-list\/1\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>, ]<\/li>\n\n\n\n<li>Move both pointers: left = 4, right = 4<\/li>\n<\/ul>\n\n\n\n<p><strong>Step 5:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>left.data = 4, right.data = 4<\/li>\n\n\n\n<li>left.data is not &lt; right.data, so loop ends<\/li>\n<\/ul>\n\n\n\n<p><strong>Result:<\/strong>&nbsp;<code>[[1], ]<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\u00a0O(n) &#8211; We visit each node at most once with our two pointers<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\u00a0O(1) &#8211; We only use a few pointer variables (excluding the result array)<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-simplifying-it\">Simplifying It<\/h3>\n\n\n\n<p>This approach is like having two people walk towards each other from opposite ends of a sorted line. They check if their combined value matches what we&#8217;re looking for. If it&#8217;s too big, the person from the high end steps back. If it&#8217;s too small, the person from the low end steps forward. If it&#8217;s just right, we record the pair and both people move inward to look for more pairs.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"8-key-insights\">Key Insights<\/h2>\n\n\n\n<p><strong>Why this works for doubly linked lists:<\/strong><br>The doubly linked list structure allows us to move both forward and backward efficiently. This is crucial for the two-pointer technique because we need to move the right pointer backward when the sum is too large.<\/p>\n\n\n\n<p><strong>Why the list needs to be sorted:<\/strong><br>The sorting property allows us to make intelligent decisions about which pointer to move. If the sum is too large, we know moving the right pointer backward will give us a smaller sum.<\/p>\n\n\n\n<p><strong>Why we check left.data &lt; right.data:<\/strong><br>This condition ensures we don&#8217;t count the same pair twice and that we stop when pointers meet or cross each other.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"9-summary\">Summary<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Approach<\/th><th>Time<\/th><th>Space<\/th><th>Difficulty<\/th><th>Best For<\/th><\/tr><\/thead><tbody><tr><td>Two Pointers<\/td><td>O(n)<\/td><td>O(1)<\/td><td>Medium<\/td><td>Production code<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>This&nbsp;<strong>optimal solution<\/strong>&nbsp;efficiently finds all pairs in a single pass using the two-pointer technique. The key insight is leveraging the sorted nature of the doubly linked list and the bidirectional traversal capability to implement an elegant solution.<\/p>\n\n\n\n<p>This problem beautifully demonstrates how the structure of doubly linked lists (with both forward and backward pointers) combined with the sorted property can lead to very efficient algorithms. The two-pointer technique is a fundamental approach that appears in many array and linked list problems!<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Join our Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\" target=\"_blank\" rel=\"noreferrer noopener\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\" target=\"_blank\" rel=\"noreferrer noopener\">+91-9712928220<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a sorted doubly linked list and a target sum, find all pairs of nodes whose sum equals the target.\u00a0Return the pairs as a list of lists, where each inner list contains two elements representing a valid pair. Here&#8217;s the [Problem Link] to begin with. Example 1: Input: 1 &lt;-&gt; 2 &lt;-&gt; 4 &lt;-&gt; 5<\/p>\n","protected":false},"author":1,"featured_media":685,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[30,19],"class_list":{"0":"post-684","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-intermediate","9":"tag-doubly-linked-list","10":"tag-medium"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/07\/find-pairs-with-given-sum-in-doubly-linked-list-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/684","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=684"}],"version-history":[{"count":1,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/684\/revisions"}],"predecessor-version":[{"id":686,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/684\/revisions\/686"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/685"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=684"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=684"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=684"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}