

Input: Linked List: 1 <-> 2 <-> 3 <-> 4 <-> 9 and 'k' = 5
Output: (1, 4) and (2, 3)
Explanation: There are 2 pairs in the linked list having sum 'k' = 5.
The first line contains a single integer 'n', the number of elements in the doubly linked list.
The second line contains 'n' integers, the elements of the doubly linked list.
The third line contains a single integer 'k', the sum the pairs should have.
The first line contains a single integer 'count', denoting the number of pairs having sum 'k'.
The next 'count' lines contains two integers each, the pair having sum 'k'. Each line will have one pair.
You don’t have to print anything, it has already been taken care of. Just return the list of the valid pairs in any order.
In the output, the first term in each pair will be smaller than the second term, and the pairs will be printed in sorted order (according to the first term).
We will find all the possible pairs by iterating over the whole doubly linked list ‘n’ times, where ‘n’ is the length of the linked list. At any time if the sum of any pair is equal to ‘k’, we will add it to our answer.
We will scan the doubly linked list from left to right and we will use the Hash to store all previously encountered elements. So at each step, we will check if ‘k’ - (current node data) already exists in Hash, if yes we can say we have found a pair.
As the given linked list is sorted so we can use two pointer technique to find the pairs in O(n) time complexity.
We will use two pointer variables ‘start’ and ‘end’ to find the candidate elements in the sorted doubly linked list.
Please note that the loop condition ‘start’ != ‘end’ is not enough, because the pointers might cross each other. It is simpler to compare their data.