Given a sorted singly linked list and a value x, the task is to find pair whose sum is equal to x. We are not allowed to use any extra space and expected time complexity is O(n).
Input : head = 3-->6-->7-->8-->9-->10-->11 , x=17 Output: (6, 11), (7, 10), (8, 9)
Hint : We are allowed to modify original linked list
A simple solution for this problem is to take each element one by one and traverse the remaining list in forward direction to find second element whose sum is equal to given value x. Time complexity for this approach will be O(n2).
An efficient solution for this problem is based on ideas discussed in below articles.
Find pair in doubly linked list : We use the same algorithm that traverses linked list from both ends.
XOR Linked list : In singly linked list, we can traverse list only in forward direction. We use XOR concept to convert a singly linked list to doubly linked list.
Below are steps :
- First we need to convert our singly linked list into doubly linked list. Here we are given singly linked list structure node which have only next pointer not prev pointer, so to convert our singly linked list into doubly linked list we use memory efficient doubly linked list ( XOR linked list ).
- In XOR linked list each next pointer of singly linked list contains XOR of next and prev pointer.
- After converting singly linked list into doubly linked list we initialize two pointers 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.
- Here we don’t have random access, so to initialize pointer, we traverse the list till last node and assign last node to 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 (first=next_node), or they become same (first == second).
(6,11) , (7,10) , (8,9)
Time complexity : O(n)
If linked list is not sorted, then we can sort the list as a first step. But in that case overall time complexity would become O(n Log n). We can use Hashing in such cases if extra space is not a constraint. The hashing based solution is same as method 2 here.
This article is contributed by Shashank Mishra ( Gullu ). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Print reverse of a Linked List without extra space and modifications
- Length of longest palindrome list in a linked list using O(1) extra space
- Find middle of singly linked list Recursively
- Find the common nodes in two singly linked list
- Find smallest and largest elements in singly linked list
- Find minimum and maximum elements in singly Circular Linked List
- Reverse a stack without using extra space in O(n)
- Clone a stack without extra space
- Find pairs with given product in a sorted Doubly Linked List
- Merge K sorted Doubly Linked List in Sorted Order
- Sorted merge of two sorted doubly circular linked lists
- Insert value in sorted way in a sorted doubly linked list
- Sum of the nodes of a Singly Linked List
- QuickSort on Singly Linked List
- Given a linked list which is sorted, how will you insert in sorted way