Find pairs with given sum in doubly linked list
Problem Statement:β
Given a sorted doubly linked list of positive distinct elements, the task is to find pairs in a doubly-linked list whose sum is equal to given valueΒ target.
-
Example:
Example 1:
Input:
1 <-> 2 <-> 4 <-> 5 <-> 6 <-> 8 <-> 9
target = 7
Output: (1, 6), (2,5)
Explanation: We can see that there are two pairs
(1, 6) and (2,5) with sum 7.
Example 2:
Input:
1 <-> 5 <-> 6
target = 6
Output: (1,5)
Explanation: We can see that there is one pairs (1, 5) with sum 6.
β Solution: Two-Pointer Technique on Doubly Linked Listβ
class Solution {
public:
vector<pair<int, int>> findPairsWithGivenSum(Node *head, int targetSum) {
vector<pair<int, int>> result;
if (head == NULL || head->next == NULL) return result;
Node* leftPointer = head;
Node* rightPointer = head;
// Move rightPointer to the end of the list
while (rightPointer->next != NULL)
rightPointer = rightPointer->next;
// Apply two-pointer approach
while (leftPointer != rightPointer && leftPointer->data <= rightPointer->data) {
int currentSum = leftPointer->data + rightPointer->data;
if (currentSum == targetSum) {
result.push_back({leftPointer->data, rightPointer->data});
leftPointer = leftPointer->next;
rightPointer = rightPointer->prev;
}
else if (currentSum > targetSum) {
rightPointer = rightPointer->prev;
}
else {
leftPointer = leftPointer->next;
}
}
return result;
}
};
π How It Worksβ
- Use two pointers: one starting from the beginning (
leftPointer), and the other from the end (rightPointer) of the doubly linked list. - The algorithm is similar to the classic two-pointer technique used on arrays, but adapted to a doubly linked list using
nextandprevpointers. - For each pair:
- If the sum equals the target, store it and move both pointers inward.
- If the sum is greater than the target, move the right pointer left.
- If the sum is less than the target, move the left pointer right.
- Terminate when both pointers meet or cross.
π§© Key Logicβ
- Two-pointer approach in a sorted doubly linked list.
- Works because the list is sorted in non-decreasing order.
β±οΈ Time & Space Complexityβ
| Complexity | Value |
|---|---|
| Time | O(N) |
| Space | O(1) extra |
β οΈ Edge Casesβ
- Empty list or only one node β return empty vector.
- No such pair exists β return empty vector.
- Multiple pairs possible β all valid pairs returned.
- Nodes can have the same value multiple times.
π‘ Other Approachesβ
| Approach | Time | Space |
|---|---|---|
| Brute Force (2 nested loops) | O(NΒ²) | O(1) |
| Hashing | O(N) | O(N) |
| Two-pointer (used here) | O(N) β | O(1) β |
π Related Problemsβ
- Two Sum II - Input array is sorted (Leetcode 167)
- Count pairs with given sum in a sorted doubly linked list
- Remove Duplicates from Sorted Linked List
- Intersection Point in Y Shaped Linked Lists
π¬