Singly Linked List in C++

What is a singly linked list in C++ programming?

Singly linked list in C++ is one of the simplest data structure. Since, we can dynamically add or delete items as per requirement, while in Arrays the size of the array once defined can not be altered.

Let us learn more about the singly linked lists in C++ programming in this article.

Singly linked list in C++

What is Singly Linked List in C++

A Singly linked List a Sequence of nodes connected to one another using pointers. Following are components of Singly Linked List –

  • Data – The value held
  • Next Pointer – Contains the address of the next node in the sequence

Unlike an array that is contiguous each node in a Singly Linked List is scattered all over memory and are connected to one another using pointers.

Singly linked list in C++
Singly linked list in C++

Structure of the singly linked list

The following set of code is used to struct the nodes of a singly linked list. Here we have defined a singly linked list that will store integer type data in it.

This code will create a data type Node, which  will be able to store two values-:

  1. int value – data
  2. pointer value – address of the next node

Insertion of a node

Singly linked list in C 3

Deletion of a node

Singly linked list in C 5
Singly linked list in C 5

Traversal in a Singly Linked List

Singly linked list in C 1

Code for Implementing Single Linked List in C++

Time and Space Complexity:

OperationClass-based ImplementationStructure-based ImplementationTime ComplexitySpace Complexity
Insert at StartinsertStart() method in classinsertStart() function with pointerO(1)O(1)
Delete from StartdeleteStart() method in classdeleteStart() function with pointerO(1)O(1)
Display Linked Listdisplay() method in classdisplay() functionO(n)O(1)
Overall Program (Insertion + Deletion + Display)Uses OOP (encapsulation, constructor)Uses procedural approach (struct & pointers)O(n)O(n)

Disadvantages of singly linked list in CPP programming

  1. Members could be assigned anywhere in the memory.
  2. Each member shall include an address size member, hence it utilizes poor memory.
  3. Some operations like reversing a list is complicated when compared with arrays,
Linked List in C++ meme 2

Why Singly Linked List?

Performing operations on a list becomes easy

Singly linked list give us the flexibility to perform various operations such as insertion, deletion in an efficient manner as compared to arrays.

Efficient memory allocation

We need not tho allocate memory in advance to the singly linked list, dynamic memory is allocated in singly linked list hence it saves extra memory .

Implentation of advance data structure

Many advance data structures are implemented with the help of singly linked list hence they become very useful there.

To Wrap It Up:

In essence, a singly linked list in C++ offers a flexible, dynamic structure where nodes can be added or removed easily, unlike static arrays. Basic operations such as insertion at the beginning, traversal, and deletion are straightforward, though random access and some more complex manipulations can become inefficient.

Despite its limitations in memory overhead and complexity for certain tasks (e.g. reversing or random access), the singly linked list remains a foundational tool. It underpins many advanced data structures and is ideal when frequent insertions or deletions are required.

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

FAQs

Singly linked lists offer flexibility in memory usage and make insertion or deletion efficient, especially when the size of data changes frequently during execution.

The major drawbacks include no backward traversal and higher memory usage due to pointer storage. Also, random access to elements is not possible, unlike arrays.

A loop in a singly linked list can be detected using Floyd’s Cycle Detection Algorithm (Tortoise and Hare method), which uses two pointers moving at different speeds.

If the head pointer is not available, you can delete the node by copying data from the next node to the current one and then deleting the next node, except for the last node.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Singly Linked List

  • Introduction to Linked List in Data Structure
  • Linked List in – C | C++ | Java
  • Singly Linked List in – C | C++ | Java
  • Insertion in singly Linked List – C | C++ | Java
    • Insertion at beginning in singly Linked List  – C | C++Java
    • Insertion at nth position in singly Linked List  – C | C++Java
    • Insertion at end in singly Linked List  – C | C++Java
  • Deletion in singly Linked List  – C | C++Java
    • Deletion from beginning in singly linked list : C | C++ | Java
    • Deletion from nth position in singly linked list : C | C++ | Java
    • Deletion from end in singly linked list : C | C++ | Java
  • Reverse a linked list without changing links between nodes (Data reverse only) – C | C++Java
  • Linked List Insertion and Deletion – C | C++Java
  • Reverse a linked list by changing links between nodes – C | C++Java
  • Linked List insertion in the middle – C | C++Java
  • Print reverse of a linked list without actually reversing – C |C++ | Java
  • Search an element in a linked list – C | C++Java
  • Insertion in a Sorted Linked List – C | C++Java
  • Delete alternate nodes of a Linked List – C | C++Java
  • Find middle of the linked list – C | C++Java
  • Reverse a linked list in groups of given size – C | C++Java
  • Find kth node from end of the linked list – C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list – C | C++Java
  • Check whether linked list is palindrome or not – C | C++Java
  • Fold a Linked List – C | C++Java
  • Insert at a given position – C | C++Java
  • Delete at a given position – C | C++Java

Singly Linked List

  • Introduction to Linked List in Data Structure
    Click Here
  • Linked List in –
    C | C++ | Java
  • Singly Linked List in –
    C | C++ | Java
  • Insertion in singly Linked List –
    C | C++ | Java
  • Insertion at beginning in singly Linked List  –
    C | C++Java
  • Insertion at nth position in singly Linked List  –
    C | C++Java
  • Insertion at end in singly Linked List  –
    C | C++Java
  • Deletion in singly Linked List  –
    C | C++Java
  • Deletion from beginning in singly linked list :
    C | C++ | Java
  • Deletion from nth position in singly linked list :
    C | C++ | Java
  • Deletion from end in singly linked list :
    C | C++ | Java
  • Linked List Insertion and Deletion –
    C | C++Java
  • Reverse a linked list without changing links between nodes (Data reverse only) –
    C | C++Java
  • Reverse a linked list by changing links between nodes –
    C | C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Insertion in the middle Singly Linked List –
    C | C++Java
  • Insertion in a Sorted Linked List –
    C | C++Java
  • Delete alternate nodes of a Linked List –
    C | C++Java
  • Find middle of the linked list –
    C | C++Java
  • Reverse a linked list in groups of given size –
    C | C++Java
  • Find kth node from end of the linked list –
    C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list –
    C | C++Java
  • Check whether linked list is palindrome or not –
    C | C++Java
  • Fold a Linked List –
    C | C++Java
  • Insert at given Position –
    C | C++Java
  • Deletion at given Position –
    C | C++Java