Given an array of size N and a Linked List where elements will be from the array but can also be duplicated, sort the linked list in the order, elements are appearing in the array. It may be assumed that the array covers all elements of the linked list.
Sorted list =
Asked in Amazon
First, make a hash table that stores the frequencies of elements in linked list. Then, simply traverse list and for each element of arr[i] check the frequency in the has table and modify the data of list by arr[i] element upto its frequency and at last Print the list.
Sort list: 5 -> 5 -> 1 -> 3 -> 2 -> 2 -> 8
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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Merge Sort for Linked Lists
- Sort an array according to the order defined by another array
- Clone a linked list with next and random pointer in O(1) space
- Sort first k values in ascending order and remaining n-k values in descending order
- Count pairs in a sorted array whose product is less than k
- Count number of triplets with product equal to given number | Set 2
- Fascinating Number
- Sum of bitwise OR of all subarrays
- Majority element in a circular array of 0’s and 1’s
- Count elements in the given range which have maximum number of divisors