Given a linked list, and a number, check if their exist two numbers whose sum is equal to given number. If there exist two numbers, print them. If there are multiple answer, print any of them.
Input : 1 -> 2 -> 3 -> 4 -> 5 -> NULL sum = 3 Output : Pair is (1, 2) Input : 10 -> 12 -> 31 -> 42 -> 53 -> NULL sum = 15 Output : NO PAIR EXIST
Iteratively check if their exist any pair or not
Method 2 (using hashing)
1. Take a hashtable and mark all element with zero
2. Iteratively mark all the element as 1 in hashtable which are present in linked list
3. Iteratively find sum-current element of linked list is present in hashtable or not
Time complexity : O(n)
Auxiliary Space : O(n)
- Check if a pair with given product exists in Linked list
- Check if elements of Linked List are present in pair
- Create new linked list from two given linked list with greater element at each node
- Difference between Singly linked list and Doubly linked list
- Merge a linked list into another linked list at alternate positions
- Convert singly linked list into circular linked list
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- Convert Singly Linked List to XOR Linked List
- Check if a linked list is Circular Linked List
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Create a linked list from two linked lists by choosing max element at each position
- Partitioning a linked list around a given value and If we don't care about making the elements of the list "stable"
- Length of longest palindrome list in a linked list using O(1) extra space
- Find pair for given sum in a sorted singly linked without extra space
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Vikash Kumar 37