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++

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

Traversal in a Singly Linked List

Singly linked list in C 1

Code for Implementing Single Linked List in C++

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.

Prime Course Trailer

Related Banners

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

Time Complexity
For Singly Linked List

Best

O(1)

Average

O(n)

Worst

O(n)

Average Comparisons

(n+1)/2

Time Complexity
For Singly Linked List

Best

O(1)

Average

O(n)

Worst

O(n)

Average Comparisons

(n+1)/2

Quiz time

Quiz Time

Question 1. Which of the following operations has a time complexity of O(1) in a singly linked list?

  1. Insertion at the beginning
  2. Deletion at the end
  3. Traversal
  4. Searching for an element

The correct answer is a)

Insertion at the beginning. In a singly linked list, insertion at the beginning involves updating only a few pointers, irrespective of the size of the list.
Therefore, the time complexity of this operation is constant, O(1). The other options require traversing the list, which takes linear time, O(n).

Question 2. What is the time complexity for searching an element in a singly linked list, assuming the element is present in the list?

  1. O(1)
  2. O(log n)
  3. O(n)
  4. O(n^2)

The correct answer is c) O(n).

Searching for an element in a singly linked list requires traversing through each node until the desired element is found or the end of the list is reached.
In the worst case, when the element is at the end of the list or not present at all, the algorithm needs to visit every node, resulting in a linear time complexity, O(n).

Question 3. What is the main disadvantage of a singly linked list compared to an array?

  1. Dynamic memory allocation
  2. Random access
  3. Insertion and deletion at the end
  4. Efficient memory utilization

The correct answer is b) Random access.

In a singly linked list, accessing an element at a specific index requires traversing the list from the beginning. This makes random access inefficient as compared to an array, where elements can be accessed directly using their indices.
Singly linked lists are better suited for operations that involve sequential access, such as traversal, insertion, and deletion.

Question 4. Which of the following operations can be performed efficiently in both singly linked lists and doubly linked lists?

  1. Insertion at the beginning
  2. Insertion at the end
  3. Deletion at the beginning
  4. Deletion at the end

The correct answer is a) Insertion at the beginning.
Both singly linked lists and doubly linked lists support efficient insertion at the beginning. In both cases, updating a few pointers is sufficient to insert a new node at the front, regardless of the size of the list.
The other options require additional traversal or pointer manipulation, resulting in slower operations.

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