VOOZH about

URL: https://www.geeksforgeeks.org/dsa/sort-linked-list-order-elements-appearing-array/

⇱ Sort the linked list in the order of elements appearing in the array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Sort the linked list in the order of elements appearing in the array

Last Updated : 11 Jul, 2025

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.
arr[] = 
 

👁 Image


list = 

👁 Image


Sorted list = 

👁 Image

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 hashtable and modify the data of list by arr[i] element upto its frequency and at last Print the list. 

Implementation:


Output
Sorted List:
5 -> 5 -> 1 -> 3 -> 2 -> 2 -> 8 -> 

Time Complexity: O(n2).
Auxiliary Space: O(n)

Comment