Given an array arr[] of size N. The task is to create linked list from the given array.
Examples:
Input : arr[]={1, 2, 3, 4, 5}
Output : 1->2->3->4->5
Input :arr[]={10, 11, 12, 13, 14}
Output : 10->11->12->13->14
Simple Approach: For each element of an array arr[] we create a node in a linked list and insert it at the end.
C++
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
void insert(Node** root, int item)
{
Node* temp = new Node;
Node* ptr;
temp->data = item;
temp->next = NULL;
if (*root == NULL)
*root = temp;
else {
ptr = *root;
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = temp;
}
}
void display(Node* root)
{
while (root != NULL) {
cout << root->data << " " ;
root = root->next;
}
}
Node *arrayToList( int arr[], int n)
{
Node *root = NULL;
for ( int i = 0; i < n; i++)
insert(&root, arr[i]);
return root;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
Node* root = arrayToList(arr, n);
display(root);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class Node
{
int data;
Node next;
};
static Node insert(Node root, int item)
{
Node temp = new Node();
Node ptr;
temp.data = item;
temp.next = null ;
if (root == null )
root = temp;
else
{
ptr = root;
while (ptr.next != null )
ptr = ptr.next;
ptr.next = temp;
}
return root;
}
static void display(Node root)
{
while (root != null )
{
System.out.print( root.data + " " );
root = root.next;
}
}
static Node arrayToList( int arr[], int n)
{
Node root = null ;
for ( int i = 0 ; i < n; i++)
root = insert(root, arr[i]);
return root;
}
public static void main(String args[])
{
int arr[] = { 1 , 2 , 3 , 4 , 5 };
int n = arr.length;
Node root = arrayToList(arr, n);
display(root);
}
}
|
Python3
import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
def insert(root, item):
temp = Node(item)
if (root = = None ):
root = temp
else :
ptr = root
while (ptr. next ! = None ):
ptr = ptr. next
ptr. next = temp
return root
def display(root):
while (root ! = None ) :
print (root.data, end = " " )
root = root. next
def arrayToList(arr, n):
root = None
for i in range ( 0 , n, 1 ):
root = insert(root, arr[i])
return root
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 4 , 5 ]
n = len (arr)
root = arrayToList(arr, n)
display(root)
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node insert(Node root, int item)
{
Node temp = new Node();
Node ptr;
temp.data = item;
temp.next = null ;
if (root == null )
root = temp;
else
{
ptr = root;
while (ptr.next != null )
ptr = ptr.next;
ptr.next = temp;
}
return root;
}
static void display(Node root)
{
while (root != null )
{
Console.Write(root.data + " " );
root = root.next;
}
}
static Node arrayToList( int []arr, int n)
{
Node root = null ;
for ( int i = 0; i < n; i++)
root = insert(root, arr[i]);
return root;
}
public static void Main(String []args)
{
int []arr = { 1, 2, 3, 4, 5 };
int n = arr.Length;
Node root = arrayToList(arr, n);
display(root);
}
}
|
Javascript
<script>
class Node {
constructor() {
var data;
var next;
}
}
function insert( root, item)
{
var temp = new Node();
var ptr;
temp.data = item;
temp.next = null ;
if (root == null )
root = temp;
else
{
ptr = root;
while (ptr.next != null )
ptr = ptr.next;
ptr.next = temp;
}
return root;
}
function display( root)
{
while (root != null )
{
document.write( root.data + " " );
root = root.next;
}
}
function arrayToList( arr, n)
{
var root = null ;
for (let i = 0; i < n; i++)
root = insert(root, arr[i]);
return root;
}
let arr = [ 1, 2, 3, 4, 5 ];
let n = arr.length;
var root = arrayToList(arr, n);
display(root);
</script>
|
Time Complexity : O(n*n)
Efficient Approach: We traverse array from end and insert every element at the beginning of the list.
C++
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
void insert(Node** root, int item)
{
Node* temp = new Node;
temp->data = item;
temp->next = *root;
*root = temp;
}
void display(Node* root)
{
while (root != NULL) {
cout << root->data << " " ;
root = root->next;
}
}
Node *arrayToList( int arr[], int n)
{
Node *root = NULL;
for ( int i = n-1; i >= 0 ; i--)
insert(&root, arr[i]);
return root;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
Node* root = arrayToList(arr, n);
display(root);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class Node
{
int data;
Node next;
};
static Node root;
static Node insert(Node root, int item)
{
Node temp = new Node();
temp.data = item;
temp.next = root;
root = temp;
return root;
}
static void display(Node root)
{
while (root != null )
{
System.out.print(root.data + " " );
root = root.next;
}
}
static Node arrayToList( int arr[], int n)
{
root = null ;
for ( int i = n - 1 ; i >= 0 ; i--)
root = insert(root, arr[i]);
return root;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 5 };
int n = arr.length;
Node root = arrayToList(arr, n);
display(root);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = next
def insert(root, item):
temp = Node( 0 )
temp.data = item
temp. next = root
root = temp
return root
def display(root):
while (root ! = None ):
print (root.data, end = " " )
root = root. next
def arrayToList(arr, n):
root = None
for i in range (n - 1 , - 1 , - 1 ):
root = insert(root, arr[i])
return root
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 4 , 5 ];
n = len (arr)
root = arrayToList(arr, n);
display(root)
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node root;
static Node insert(Node root, int item)
{
Node temp = new Node();
temp.data = item;
temp.next = root;
root = temp;
return root;
}
static void display(Node root)
{
while (root != null )
{
Console.Write(root.data + " " );
root = root.next;
}
}
static Node arrayToList( int []arr, int n)
{
root = null ;
for ( int i = n - 1; i >= 0 ; i--)
root = insert(root, arr[i]);
return root;
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 4, 5 };
int n = arr.Length;
Node root = arrayToList(arr, n);
display(root);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
var root;
function insert(root, item) {
var temp = new Node();
temp.data = item;
temp.next = root;
root = temp;
return root;
}
function display(root) {
while (root != null ) {
document.write(root.data + " " );
root = root.next;
}
}
function arrayToList(arr, n) {
root = null ;
for ( var i = n - 1; i >= 0; i--) root = insert(root, arr[i]);
return root;
}
var arr = [1, 2, 3, 4, 5];
var n = arr.length;
var root = arrayToList(arr, n);
display(root);
</script>
|
Time Complexity : O(n)
Alternate Efficient Solution is maintain tail pointer, traverse array elements from left to right, insert at tail and update tail after insertion.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!