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

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

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.

`// Efficient CPP program to sort given list in order ` `// elements are appearing in an array ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Linked list node ` `struct` `Node { ` ` ` `int` `data; ` ` ` `struct` `Node* next; ` `}; ` ` ` `// function prototype for printing the list ` `void` `printList(` `struct` `Node*); ` ` ` `// Function to insert a node at the ` `// beginning of the linked list ` `void` `push(` `struct` `Node** head_ref, ` `int` `new_data) ` `{ ` ` ` `struct` `Node* new_node = ` `new` `Node; ` ` ` `new_node -> data = new_data; ` ` ` `new_node -> next = *head_ref; ` ` ` `*head_ref = new_node; ` `} ` ` ` `// function to print the linked list ` `void` `printList(` `struct` `Node* head) ` `{ ` ` ` `while` `(head != NULL) { ` ` ` `cout << head -> data << ` `" -> "` `; ` ` ` `head = head -> next; ` ` ` `} ` `} ` ` ` `// Function that sort list in order of apperaing ` `// elements in an array ` `void` `sortlist(` `int` `arr[], ` `int` `N, ` `struct` `Node* head) ` `{ ` ` ` `// Store frequencies of elements in a ` ` ` `// hash table. ` ` ` `unordered_map<` `int` `, ` `int` `> hash; ` ` ` `struct` `Node* temp = head; ` ` ` `while` `(temp) { ` ` ` `hash[temp -> data]++; ` ` ` `temp = temp -> next; ` ` ` `} ` ` ` ` ` `temp = head; ` ` ` ` ` `// One by one put elements in lis according ` ` ` `// to their appearance in array ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` ` ` `// Update 'frequency' nodes with value ` ` ` `// equal to arr[i] ` ` ` `int` `frequency = hash[arr[i]]; ` ` ` `while` `(frequency--) { ` ` ` ` ` `// Modify list data as element ` ` ` `// appear in an array ` ` ` `temp -> data = arr[i]; ` ` ` `temp = temp -> next; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `struct` `Node* head = NULL; ` ` ` `int` `arr[] = { 5, 1, 3, 2, 8 }; ` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `// creating the linked list ` ` ` `push(&head, 3); ` ` ` `push(&head, 2); ` ` ` `push(&head, 5); ` ` ` `push(&head, 8); ` ` ` `push(&head, 5); ` ` ` `push(&head, 2); ` ` ` `push(&head, 1); ` ` ` ` ` `// Function call to sort the list in order ` ` ` `// elements are apperaing in an array ` ` ` `sortlist(arr, N, head); ` ` ` ` ` `// print the modified linked list ` ` ` `cout << ` `"Sorted List:"` `<< endl; ` ` ` `printList(head); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output :

Sort list: 5 -> 5 -> 1 -> 3 -> 2 -> 2 -> 8

## Recommended Posts:

- Replace even nodes of a doubly linked list with the elements of array
- Sort even and odd placed elements in increasing order
- Sort even-placed elements in increasing and odd-placed in decreasing order
- Sort a linked list of 0s, 1s and 2s
- Sort an array where a subarray of a sorted array is in reverse order
- Print Doubly Linked list in Reverse Order
- Partitioning a linked list around a given value and keeping the original order
- Sort Linked List containing values from 1 to N
- Partitioning a linked list around a given value and If we don't care about making the elements of the list "stable"
- Traverse Linked List from middle to left-right order using recursion
- Sort a linked list of 0s, 1s and 2s by changing links
- Sort the biotonic doubly linked list | Set-2
- Iterative Merge Sort for Linked List
- Bubble Sort On Doubly Linked List
- Iterative selection sort for linked list

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.