VOOZH about

URL: https://www.geeksforgeeks.org/dsa/given-a-linked-list-which-is-sorted-how-will-you-insert-in-sorted-way/

⇱ Insert in a Sorted List - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Insert in a Sorted List

Last Updated : 29 Apr, 2026

Given a linked list sorted in ascending order and an integer called key, insert data in the linked list such that the list remains sorted.

Input: Linked List: 25->36->47->58->69->80, key: 19
Output: 19->25->36->47->58->69->80
Explanation: After inserting 19 the sorted linked list will look like the one in the output.
👁 420047172

Input: Linked List: 50->100, key: 75
Output: 50->75->100
Explanation: After inserting 75 the sorted linked list will look like the one in the output.
👁 420047174

[Expected Approach] Traverse and find the correct position for insertion - O(n) Time and O(1) Space

  1. If Linked list is empty then make the node as head and return it.
  2. If key < head->data, then insert the node at the start, make it head and return.
  3. In a loop, find the appropriate node after which the input node (let 9) is to be inserted. To find the appropriate node start from the head, keep moving until you reach a node who's next is greater than the key.
  4. Create a new node and make it next of the node found in the previous step.



Output
19 25 36 47 58 69 80 
Comment