Find pairs with given sum in doubly linked list - GeeksforGeeks
Given a sorted doubly linked list of positive distinct elements, the task is to find pairs in doubly linked list whose sum is equal to given value x, without using any extra space ?
Read full article from Find pairs with given sum in doubly linked list - GeeksforGeeks
Given a sorted doubly linked list of positive distinct elements, the task is to find pairs in doubly linked list whose sum is equal to given value x, without using any extra space ?
A simple approach for this problem is to one by one pick each node and find second element whose sum is equal to x in the remaining list by traversing in forward direction.Time complexity for this problem will be O(n^2) , n is total number of nodes in doubly linked list.
An efficient solution for this problem is same as this article. Here is the algorithm :
- Initialize two pointer variables to find the candidate elements in the sorted doubly linked list.Initialize first with start of doubly linked list i.e; first=head and initialize second with last node of doubly linked list i.e; second=last_node.
- We initialize first and second pointers as first and last nodes. Here we don’t have random access, so to find second pointer, we traverse the list to initialize second.
- If current sum of first and second is less than x, then we move first in forward direction. If current sum of first and second element is greater than x, then we move second in backward direction.
- Loop termination conditions are also different from arrays. The loop terminates when either of two pointers become NULL, or they cross each other (second->next = first), or they become same (first == second)
void
pairSum(
struct
Node *head,
int
x)
{
// Set two pointers, first to the beginning of DLL
// and second to the end of DLL.
struct
Node *first = head;
struct
Node *second = head;
while
(second->next != NULL)
second = second->next;
// To track if we find a pair or not
bool
found =
false
;
// The loop terminates when either of two pointers
// become NULL, or they cross each other (second->next
// == first), or they become same (first == second)
while
(first != NULL && second != NULL &&
first != second && second->next != first)
{
// pair found
if
((first->data + second->data) == x)
{
found =
true
;
cout <<
"("
<< first->data<<
", "
<< second->data <<
")"
<< endl;
// move first in forward direction
first = first->next;
// move second in backward direction
second = second->prev;
}
else
{
if
((first->data + second->data) < x)
first = first->next;
else
second = second->prev;
}
}
// if pair is not present
if
(found ==
false
)
cout <<
"No pair found"
;
}