### Introduction

The linked list is one of the most important concepts and 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.

A Linked List is a linear data structure. In linked list elements are not stored at contiguous locations. Linked list contain nodes and these nodes are linked using pointers. The various types of linked lists are explained below:

• Each node contains data and has a pointer that will point to the next node of the same datatype.
• We can traverse the list in one way only in forward direction. #### Structure of Singly Linked List Node

```class Node {
public:
int data;
// Pointer to next node in LL
Node* next;
};
```

#### Creation and Traversal of list

```#include
using namespace std;

// Structure of Node
class Node {
public:
int data;
Node* next;
Node(int x){
data = x;
next = NULL;
}
};

// This function will print data present in the linked list from
// start to end
void printList(Node* n)
{

// move till n is NULL
while (n != NULL) {

// Print the data
cout << n->data << " ";
n = n->next;
}
}

// Driver Code
int main()
{
Node* second = NULL;
Node* third = NULL;

// create 3 nodes with data
second = new Node(12);
third = new Node(37);

// create a linke from first node to second node

// create a linke from second node to third node
second->next = third;

// make last node’s next as NULL
third->next = NULL;

return 0;
}
```

#### Output

10 12 37

Time complexity: O(n) for printList function where ‘n’ is the number of nodes present in the list
Space complexity: O(n), where ‘n’ is the number of nodes present in the list

• Each node contains data and has two pointers named next and previous. The next pointer points to the next node and the previous one will point to the previous node.
• We can traverse in both directions in this list. #### Structure of Doubly Linked List Node

```class Node {
public:
int data;
// Pointer to next node in LL
Node* next,*prev;
Node(int x){
data = x;
next = NULL;
prev = NULL;
}
};
```

### Creation and Traversal of list

```#include
using namespace std;

class Node {
public:
int data;
Node* next;
Node* prev;
Node(int x){
data = x;
prev = NULL;
next = NULL;
}
};

// This function will add a new node in front of the list
{
// create a new node with given data
Node* new_node = new Node(new_data);

// assign NULL to prev of new node
new_node->prev = NULL;

// update the previous of old node as new node

// update the head node of list as new node
}

// This function will print data present in the linked list from
// start to end and also in reverse order
void printList(Node* node)
{
Node* last;

cout << "\nTraversal in forward"
<< " direction \n";
while (node != NULL) {

// Print the data
cout << " " << node->data << " ";
last = node;
node = node->next;
}

cout << "\nTraversal in reverse"
<< " direction \n";
while (last != NULL) {

// Print the data
cout << " " << last->data << " ";
last = last->prev;
}
}

// Driver Code
int main()
{

// Insert 10.
// So linked list becomes 10->NULL

// Insert 44 at the beginning. So

// Insert 11 at the beginning. So

cout << "Created DLL is: ";

return 0;
}
```

#### Output

Created DLL is:
Traversal in forward direction
11 44 10
Traversal in reverse direction
10 44 11

Time Complexity:

• Push function: O(1)
• printList function: O(n), where ‘n’ is the number of nodes present in the list

Space Complexity: O(n), where ‘n’ is the number of nodes present in the list

• Each node contains data and a pointer that points to the next node.
• A special property of this list is that the last node will point to the first node which makes it look circular and we can traverse the full linked list starting from any node. #### Structure of Circular Linked List Node

```class Node {
public:
int data;
// Pointer to next node in LL
Node* next;
};
```

#### Creation and Traversal of list

```#include
using namespace std;

// Structure for a node
class Node {
public:
int data;
Node* next;
Node(int x){
data = x;
next = NULL;
}
};

// This function will insert a node at the beginning of the
// list
{
Node* ptr1 = new Node(data);

temp = temp->next;
}
temp->next = ptr1;
}

else
ptr1->next = ptr1;

}

// This function will print data of all nodes in the list
{
do {

// Print the data
cout << temp->data << " ";
temp = temp->next;
}
}

// Main function
int main()
{
//created list will be 9->5->35->14
cout << "Contents of Circular"

return 0;
}
```

#### Output

9 5 35 14

Time complexity

• Push function: O(n), where ‘n’ is the number of nodes present in the list.
• printList function: O(n), where ‘n’ is the number of nodes present in the list.

Space Complexity: O(n), where ‘n’ is the number of nodes present in the list

• Each node contains data and has two pointers named next and previous.
• The next pointer points to the next node and the previous one will point to the previous node.
• Apart from this, the last node’s next pointer will point to the first node and the first node’s previous pointer will point to the last node. #### Structure of Doubly Circular Linked List Node

```class Node {
public:
int data;
// Pointer to next node in LL
Node* next,*prev;
Node(int x){
data = x;
next = NULL;
prev = NULL;
}
};
```

#### Creation and Traversal of list

```#include
using namespace std;

// Structure of a Node
class Node {
public:
int data;
// Pointer to next node in LL
Node* next,*prev;
Node(int x){
data = x;
next = NULL;
prev = NULL;
}

};

// This function will insert a node at the beginning of the
// list
void insertBegin(Node** start,
int value)
{
// when list is empty
if (*start == NULL) {
Node* new_node = new Node(value);
new_node->next
= new_node->prev = new_node;
*start = new_node;
return;
}

// last node
Node* last = (*start)->prev;

Node* new_node = new Node(value);

// update the prev and next pointers
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;
}

// This function will print the data in nodes of our list
void display(Node* start)
{
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()
{
Node* start = NULL;

// Insert 7
// So linked list becomes 7->NULL
insertBegin(&start, 7);

// Insert 5 at the beginning
// So linked list becomes 5->7
insertBegin(&start, 5);

// Insert 10 at the end
// So linked list becomes 10->5->7
insertBegin(&start, 10);

printf("Created circular doubly"
display(start);

return 0;
}
```

#### Output

Created circular doubly linked list is:
Traversal in forward direction
10 5 7
Traversal in reverse direction
7 5 10

Time Complexity

• insertBegin function: O(1)
• Display function: O(n), where ‘n’ is the number of nodes present in the list

Space Complexity: O(n), where ‘n’ is the number of nodes present in the list

So, in this blog, we have tried to explain different types of Linked lists. If you want to solve more questions on Linked List, which are curated by our expert mentors at PrepBytes, you can follow this link Linked List.