Find Pairs with a Given Sum in a Doubly Linked List
Solve this ProblemTest Case 1:
Test Case 2:
Test Case 3:
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
GoodFor 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.
O(n²)O(1) extra1// 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
OptimalSince 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.
O(n)O(1) extra1// 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}