Find Pairs with a Given Sum in a Doubly Linked List

Solve this Problem
Medium20–25 min
Topics
Companies
Practice:GFG ↗
Given the head of a doubly linked list sorted in non-decreasing order and an integer target, find all pairs of nodes whose values add up to target — each node used in at most one pair — and return the pairs. Because the list is already sorted, this is the linked-list version of the classic "two-sum on a sorted array" two-pointer trick — except walking backward from the tail uses a real .prev pointer instead of an index, which is exactly the kind of operation a doubly linked list makes cheap that a singly linked list can't do at all.

Test Case 1:

Input:head = [1, 2, 4, 5, 6, 8, 9], target = 10
Output:[[1, 9], [2, 8], [4, 6]]
Explanation:Three pairs sum to 10; 5 is left over with no partner.

Test Case 2:

Input:head = [1, 2, 3], target = 10
Output:[]
Explanation:No pair reaches the target sum, so the answer is empty.

Test Case 3:

Input:head = [-3, -1, 0, 2, 5], target = 2
Output:[[-3, 5], [0, 2]]
Explanation:Negative values work the same way — -3 + 5 = 2 and 0 + 2 = 2.

Constraints

  • 0 ≤ number of nodes in head ≤ 200
  • -1000 ≤ node value, target ≤ 1000
  • head is sorted in non-decreasing order
  • each node may be used in at most one pair
🚀

Try the Dry Run

Don't just read the solution — watch it execute, one step at a time.

Approach & Solutions

Brute Force — Check Every Pair

Good

For every node, walk every node after it and check whether the two values add up to the target. It's simple and doesn't need the list to be sorted, but it re-examines every later node for every earlier one — quadratic work even though the list is already sorted and a much faster scan is possible.

TimeO(n²)
SpaceO(1) extra
1// Node definition used in this problem: 2// class Node { 3// int val; 4// Node next; 5// } 6 7class Solution { 8 public int[][] findPairsSum(Node head, int target) { 9 List<int[]> pairs = new ArrayList<>(); 10 for (Node a = head; a != null; a = a.next) { 11 for (Node b = a.next; b != null; b = b.next) { 12 if (a.val + b.val == target) { 13 pairs.add(new int[]{a.val, b.val}); 14 break; 15 } 16 } 17 } 18 return pairs.toArray(new int[0][]); 19 } 20}

Optimal — Two Pointers from Both Ends

Optimal

Since the list is sorted, a left pointer starting at head and a right pointer starting at the tail can close in on each other, exactly like the two-pointer technique on a sorted array. If the sum at left + right is too small, left moves forward (to grow the sum); if it's too big, right moves backward (to shrink it); if it's exactly the target, that pair is recorded and both pointers move in — one pass covers the whole list. Walking backward from the tail needs a real .prev pointer, so an internal doubly linked copy (with .prev) is built first from the input chain — and since there's no index to compare positions with, a running count of how many nodes are still left to check (instead of comparing pointers) is what tells the loop when to stop.

TimeO(n)
SpaceO(1) extra
1// Node definition used in this problem: 2// class Node { 3// int val; 4// Node next; 5// } 6 7class Solution { 8 static class DNode { 9 int val; 10 DNode next; 11 DNode prev; 12 DNode(int val) { this.val = val; } 13 } 14 15 public int[][] findPairsSum(Node head, int target) { 16 DNode dHead = null, dTail = null; 17 int n = 0; 18 Node curr = head; 19 while (curr != null) { 20 DNode node = new DNode(curr.val); 21 if (dHead == null) { 22 dHead = node; 23 } else { 24 node.prev = dTail; 25 dTail.next = node; 26 } 27 dTail = node; 28 n++; 29 curr = curr.next; 30 } 31 32 List<int[]> pairs = new ArrayList<>(); 33 DNode left = dHead; 34 DNode right = dTail; 35 int remaining = n; 36 while (remaining > 1) { 37 int sum = left.val + right.val; 38 if (sum == target) { 39 pairs.add(new int[]{left.val, right.val}); 40 left = left.next; 41 right = right.prev; 42 remaining -= 2; 43 } else if (sum < target) { 44 left = left.next; 45 remaining--; 46 } else { 47 right = right.prev; 48 remaining--; 49 } 50 } 51 return pairs.toArray(new int[0][]); 52 } 53}

Related Problems