Implement Sorted Singly Linked List in C++



A singly linked list is a type of linear data structure where each node contains two items: The data and a link to the next node in the list. Where, first node is called as head node and the last node is called the tail.

So, you can traverse the linked list from the head node and continue until the link becomes null.

Sorted Singly Linked List

A singly linked list is supposed to be sorted when its elements are arranged in specific order (i.e., ascending or descending order), and each node contains data and a pointer (or reference) to the next node. Let's understand through a diagram:

Sorted-Singly-LinkedList

Implementation of Sorted Singly Linked List

Below is the step-by-step explanation to implement a sorted singly linked list:

Step1: Node Structure

We create a node structure. Each node will hold an integer of data and a pointer to the next node.

struct Node {
   // Data part of the node
   int data;
   // Pointer to the next node
   struct Node* next;
};

Step2: Function to Insert Node in Sorted Order

We can insert the node in the sorted order. If the node is smaller than all nodes, then it will insert at first and become the head node if the order is ascending. If the node is larger than all, it will insert last, etc.

Suppose I want to insert 5 and 35 in the sorted linked list: See the diagram below:

Sorted-Singly-LinkedList
void insertNode(Node*& head, int value) {
   Node* newNode = new Node();
   newNode->data = value;
   newNode->next = nullptr;

   if (!head || value < head->data) {
      // Insert at head
      newNode->next = head;
      head = newNode;
      return;
   }

   Node* current = head;
   while (current->next && current->next->data < value) {
      current = current->next;
   }

   newNode->next = current->next;
   current->next = newNode;
}

Step3: Display The Node

To display the linked list, we start from the head and traverse through each node, printing its data until we reach NULL.

void display(Node* head) {
   cout << "Sorted Linked List: ";
   while (head) {
      cout << head->data << " -> ";
      head = head->next;
   }
   cout << "NULL\n";
}

Implementation Of Sorted Singly LinkedList in C++

Following is the example implementation of a sorted singly linked list using all the above steps:

Open Compiler
#include <iostream> using namespace std; struct Node { int data; Node * next; }; // Function to insert a node in a sorted manner void insertNode(Node * & head, int value) { Node * newNode = new Node(); newNode -> data = value; newNode -> next = nullptr; if (!head || value < head -> data) { // Insert at head newNode -> next = head; head = newNode; return; } Node * current = head; while (current -> next && current -> next -> data < value) { current = current -> next; } newNode -> next = current -> next; current -> next = newNode; } // Function to display the linked list void display(Node * head) { cout << "Sorted Linked List: "; while (head) { cout << head -> data << " -> "; head = head -> next; } cout << "NULL\n"; } // Main function int main() { Node * head = nullptr; int values[] = {10, 3, 7, 1, 9, 5}; int size = sizeof(values) / sizeof(values[0]); for (int i = 0; i < size; i++) { insertNode(head, values[i]); } display(head); return 0; }

Following is the output of the above code:

Sorted Linked List: 1 -> 3 -> 5 -> 7 -> 9 -> 10 -> NULL
Updated on: 2025-05-22T18:10:45+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements