How to sort a singly linked list using insertion
A linked list is a data structure made up of one or more than one nodes. Each node contains a data value and a pointer to the next node, forming a chain-like structure. These nodes are stored randomly in the system's memory, which improves its space complexity compared to an array.
Understanding the problem
Several sorting methods can be used to sort a linked list. This Answer explores how a linked list can be sorted using insertion. Insertion sort works the same as we sort the play cards in our hands. It compares the two elements and swaps their position depending on their values. It keeps on swapping until we get a sorted list.
Note: Click here to learn more about insertion sort.
Solution
First of all, we create an empty list. Now, we start traversing the given linked list. Then, we insert each node in a sorted way in the new list. We keep doing this until we reach the list's tail. Now, we change the head of the given list to the new sorted list.
Let's visualize an example:
Implementation
Here's the code implementation of the solution above in C++:
// function for inserting node in a sorted wayvoid sortedInsert(node**, node*);// function for insertion sort (linked list)node insertionSort(node *head){// Initialize sorted and currentnode *sorted = NULL;node *current = head;while (current != NULL){// Store address for next iterationnode *link = current->link;// Insert current in new linked listsortedInsert(&sorted, current);// Update currentcurrent = link;}// Update head*head = *sorted;return *head;}// Function to insert a new nodevoid sortedInsert(node** head, node* new_node){node* current;// For headif (*head == NULL || (*head)->data >= new_node->data){new_node->link = *head;*head = new_node;}else{ // Locating node (tail)current = *head;while (current->link!=NULL &¤t->link->data < new_node->data){current = current->link;}// Updating linknew_node->link = current->link;current->link = new_node;}}int main(){node* list1=new node();list1 = NULL;list1=add_node(list1,2);add_node(list1,8);add_node(list1,5);add_node(list1,6);cout << "Linked List: ";print_data(list1);cout<<"\nAfter Sorting: ";insertionSort(list1);print_data(list1);return 0;}
Explanation
Lines 4–8: We define the
insertionSort()function to perform insertion sort on the given linked list. We initialize two pointer variables,sortedandcurrent, to keep track of the list.Lines 10–17: In the
whileloop, we save thelinkof the current node, which has the address of the next node. We call thesortedInsert()function to insert thecurrentnode into thesortedlinked list. Then, we update thecurrentfor the next iteration.Lines 19–22: We store the updated
sortedlinked list in theheadof the given linked list and returnhead.Lines 24–31: We define the
sortedInsert()function to add each node in a sorted way in the new linked list. First, we declare thecurrentpointer to keep track of the list. Then, in theifcondition, we check theheadand update it accordingly.Lines 33–39: In the
elsepart, we check the givendataand find the tail of the list using thewhileloop. In the loop, we update thelinkof thecurrentfor the next iteration.Lines 40–45: After finding the tail of the link list, we update the value of the
currentnode with thenew_node.
Time complexity: The time complexity for this approach is
Space complexity: The space complexity for this approach is
Free Resources