# Create Linked List From A Given Array

## Introduction

Sometimes, we prefer a linked list over an array as insertion and deletion of elements in the linked list is faster than array, and we need not define the linked list’s size unlike, an array. So, it is necessary to know how we can create a linked list from an array as it has more real-world implementations. Before we get started on this, let's brush up our knowledge of linked lists with this short video.

## Problem statement

You are given an array. Create a linked list from the given array.

Input: arr[]={7,4,3,2,5}

Output:7->4->3->2->5

Note: Please try to solve the problem first and then see the solution below.

## Approaches

There are mainly two methods that can be used to create a linked list from an array.

### Method 1

In this approach, we will insert a new node at the end of the linked list for each element of the array.

• First, allocate memory for  the new node
• Then find the last node of the linked list
• add the new node after the last node

``````#include<bits/stdc++.h>
using namespace std;

struct node{
int data;
node* next;

};

//function for inserting a new node at the end
{
//allocate memory for new node

node* new_node= new node;
new_node->data=value;
new_node->next=NULL;

//finding the last node
{
}
else{

while(tmp->next!=NULL)
{
tmp=tmp->next;
}
// adding the new node after the last node
tmp->next=new_node;
}
}

//function for printing the linked list

{
{

}

}

int main()
{
int arr[]={7,4,3,2,5};

for(int i=0;i<5;i++)
{
}

}``````

Output

The above approach takes O( n2) time in the worst case where n is the size of the given array. We need to traverse the whole linked list, every time we are adding a new node in the linked list.

### Method 2

In this approach, we will start iterating the given array from the end and add the new node in front of the last added node(i.e., at the beginning of the linked list).

• First, allocate memory for  the new node

``````#include<bits/stdc++.h>
using namespace std;

struct node {
int data;
node* next;

};

//function for inserting a new node at the beginning
//allocate memory for new node

node* new_node= new node;
new_node->data=value;

// adding new node at the beginning

}

//function for printing the linked list
{

}

}

//driver code
int main() {
int arr[]={7,4,3,2,5};

for(int i=4;i>=0;i--) {
}

}``````

Output

The above approach takes O(n) time in the worst case where n is the size of the given array. So, it is an optimized approach for creating a linked list from a given array.

For both methods, space complexity is O(n) as we are allocating memory for each node.

1). What is a linked list?

A linked list is a dynamic data structure where each element (called a node) is made up of two items: the data and a reference (or pointer), which points to the next node. A linked list is a collection of nodes where each node is connected to the next node through its pointer.

2). How many types of linked lists are there?

There are mainly four types of linked lists.

• We don’t need to specify the size of the linked list while declaration. So it is a dynamic data structure, but an array is a static data structure with a fixed length.
•  Insertion and deletion of elements in the linked list are faster than array.

## Conclusion

This article covered how to create a linked list from a given array. A good grasp of Linked Lists can be a huge plus point in a coding interview.

Check out the CodeStudio library for getting a better hold of the data structures and algorithms.

Side by side, you can also practice a wide variety of coding questions commonly asked in interviews in CodeStudio. Along with coding questions, you can also find the interview experience of scholars working in renowned product-based companies here.

Happy learning! 