Difference between singly and circular linked list

Difference between singly and circular linked list

Introduction

The linked list is one of the most important data structures to learn while preparing for interviews. Having a good grasp of Linked Lists can be a huge plus point in a coding interview.

Singly Linked List Introduction

A singly linked list is the kind of linked list, in which a node has two fields:

The data field stores the data or the information, and the next field stores the address of the next node.

Difference between singly and circular linked list

Doubly Linked List Introduction

A doubly linked list is the kind of linked list, in which a node has three fields:

The data field stores the data or the information and the next field stores the address of the next node and the previous field stores the address of the previous node.

Difference between singly and circular linked list

Difference between Singly linked list and Doubly linked list

Singly Linked List Doubly Linked List
A Singly Linked has nodes with a data field and a next link field. A Doubly Linked List has a previous link field along with a data field and a next link field.
In a Singly Linked List, the traversal can only be done using the link of the next node. In a Doubly Linked List, the traversal can be done using the next node link as well as the previous node link.
A Singly Linked List occupies less memory than the Doubly Linked List, as it has only 2 fields. A Doubly Linked List occupies more memory than the Singly Linked List, as it has 3 fields.
Accessing elements in a Singly Linked List is less efficient when compared to a Doubly Linked List, as only forward traversal is possible. Accessing elements in a Doubly Linked List is more efficient when compared to a Singly Linked List as both forward and backward traversal is possible.
The time complexity of inserting or deleting a node at a given position (if the pointer to that position is given) in a singly linked list is O(n). The time complexity of inserting or deleting a node at a given position (if the pointer to that position is given) in a doubly linked list is O(1).
Singly linked list is preferred when we have memory limitation(we can’t use much memory) and searching is not required. Doubly linked list is preferred when we don’t have memory limitation and searching is required(we need to perform search operation on the linked list).

So, in this article, we have tried to explain the differences between a Singly Linked List and a Doubly Linked List. These basic concepts should be clear if we want to do good in a coding interview. If you want to solve more questions on singly and doubly linked List, which are curated by our expert mentors at PrepBytes, you can follow this link Linked List.

// C++ program to illustrate creation

// & traversal of Doubly Circular LL

#include <bits/stdc++.h>

using namespace std;

// Structure of a Node

struct Node {

int data;

struct Node* next;

struct Node* prev;

};

// Function to insert Node at

// the beginning of the List

void insertBegin[struct Node** start,

int value]

{

// If the list is empty

if [*start == NULL] {

struct Node* new_node = new Node;

new_node->data = value;

new_node->next

= new_node->prev = new_node;

*start = new_node;

return;

}

// Pointer points to last Node

struct Node* last = [*start]->prev;

struct Node* new_node = new Node;

// Inserting the data

new_node->data = value;

// Update the previous and

// next of new node

new_node->next = *start;

new_node->prev = last;

// Update next and previous

// pointers of start & last

last->next = [*start]->prev

= new_node;

// Update start pointer

*start = new_node;

}

// Function to traverse the circular

// doubly linked list

void display[struct Node* start]

{

struct Node* temp = start;

printf["\nTraversal in"

" forward direction \n"];

while [temp->next != start] {

printf["%d ", temp->data];

temp = temp->next;

}

printf["%d ", temp->data];

printf["\nTraversal in "

"reverse direction \n"];

Node* last = start->prev;

temp = last;

while [temp->prev != last] {

// Print the data

printf["%d ", temp->data];

temp = temp->prev;

}

printf["%d ", temp->data];

}

// Driver Code

int main[]

{

// Start with the empty list

struct Node* start = NULL;

// Insert 5

// So linked list becomes 5->NULL

insertBegin[&start, 5];

// Insert 4 at the beginning

// So linked list becomes 4->5

insertBegin[&start, 4];

// Insert 7 at the end

// So linked list becomes 7->4->5

insertBegin[&start, 7];

printf["Created circular doubly"

" linked list is: "];

display[start];

return 0;

}

Introduction to Singly linked list : A singly linked list is a set of nodes where each node has two fields ‘data’ and ‘link’. The ‘data’ field stores actual piece of information and ‘link’ field is used to point to next node. Basically the ‘link’ field stores the address of the next node.



Difference between singly and circular linked list
png" alt="linkedlist">

Introduction to Doubly linked list : A Doubly Linked List (DLL) contains an extra pointer, typically called previous pointer, together with next pointer and data which are there in singly linked list. 

Singly linked list vs Doubly linked list 
 

Singly linked list (SLL) Doubly linked list (DLL)  
SLL nodes contains 2 field -data field and next link field. DLL nodes contains 3 fields -data field, a previous link field and a next link field.  
Difference between singly and circular linked list
Difference between singly and circular linked list
 
In SLL, the traversal can be done using the next node link only. Thus traversal is possible in one direction only. In DLL, the traversal can be done using the previous node link or the next node link. Thus traversal is possible in both directions (forward and backward).  
The SLL occupies less memory than DLL as it has only 2 fields. The DLL occupies more memory than SLL as it has 3 fields.  
Complexity of insertion and deletion at a given position is O(n).  Complexity of insertion and deletion at a given position is O(n / 2) = O(n) because traversal can be made from start or from the end.  
Complexity of deletion with a given node is O(n), because the previous node needs to be known, and traversal takes O(n) Complexity of deletion with a given node is O(1) because the previous node can be accessed easily  
We mostly prefer to use singly linked list for the execution of stacks. We can use a doubly linked list to execute heaps and stacks, binary trees.  
When we do not need to perform any searching operation and we want to save memory, we prefer a singly linked list. In case of better implementation, while searching, we prefer to use doubly linked list.  
A singly linked list consumes less memory as compared to the doubly linked list. The doubly linked list consumes more memory as compared to the singly linked list.  

Practice Tags :