Create linked list from a given array (original) (raw)

Last Updated : 27 Aug, 2025

Given an array **arr[]. Create linked list from the given array.
**Examples:

**Input: arr[] = [1, 2, 3, 4, 5]
**Output: 1 -> 2 -> 3 -> 4 -> 5
**Explanation: With the given arr[] the linked list is represented as

link1

**Input: arr[] = [10, 11, 12, 13, 14]
**Output: 10 -> 11 -> 12 -> 13 -> 14
**Explanation: With the given arr[] the linked list is represented as

link2

Try It Yourselfredirect icon

[Naive Approach] Sequential insertion at the end of a linked list (using traversal)

For each element of an array **arr[] we create a node in a linked list and insert it at the end.

C++ `

#include #include using namespace std;

// Definition of a Node in a singly linked list class Node { public: int data; Node* next;

Node(int d) {
    data = d;
    next = NULL;
}

};

// Function to insert node at the end Node* insertEnd(Node* root, int item) { Node* temp = new Node(item); if (root == NULL) return temp;

Node* last = root;
while (last->next != NULL) {
    last = last->next;
}

last->next = temp;
return root;

}

// Function to convert vector to linked list Node* arrayToList(vector& arr, int n) { Node* root = NULL; for (int i = 0; i < n; i++) { root = insertEnd(root, arr[i]); } return root; }

// Function to display linked list void display(Node* root) { while (root != NULL) { cout << root->data; if (root->next != NULL) cout << " -> "; root = root->next; } }

int main() { vector arr = {1, 2, 3, 4, 5}; int n = arr.size(); Node* root = arrayToList(arr, n); display(root); return 0; }

Java

class Node { int data; Node next;

Node(int d) {
    data = d;
    next = null;
}

}

class GfG {

// Function to insert node at the end
static Node insertEnd(Node root, int item) {
    Node temp = new Node(item);
    if (root == null)
        return temp;

    Node last = root;
    while (last.next != null) {
        last = last.next;
    }

    last.next = temp;
    return root;
}

// Function to convert array to linked list
static Node arrayToList(int arr[], int n) {
    Node root = null;
    for (int i = 0; i < n; i++) {
        root = insertEnd(root, arr[i]);
    }
    return root;
}

static void display(Node root) {
    while (root != null) {
        System.out.print(root.data);
        if (root.next != null)
            System.out.print(" -> ");
        root = root.next;
    }
}

public static void main(String[] args) {
    int arr[] = { 1, 2, 3, 4, 5 };
    int n = arr.length;
    Node root = arrayToList(arr, n);
    display(root);
}

}

Python

class Node: def init(self, d): self.data = d self.next = None

Function to insert node at the end

def insertEnd(root, item): temp = Node(item) if root is None: return temp

last = root
while last.next is not None:
    last = last.next

last.next = temp
return root

Function to convert array to linked list

def arrayToList(arr, n): root = None for i in range(n): root = insertEnd(root, arr[i]) return root

def display(root): while root is not None: print(root.data, end="") if root.next is not None: print(" -> ", end="") root = root.next

if name == "main": arr = [1, 2, 3, 4, 5] n = len(arr) root = arrayToList(arr, n) display(root)

C#

using System;

class Node { public int data; public Node next;

public Node(int d) {
    data = d;
    next = null;
}

}

class GfG {

// Function to insert node at the end
static Node insertEnd(Node root, int item) {
    Node temp = new Node(item);
    if (root == null)
        return temp;

    Node last = root;
    while (last.next != null) {
        last = last.next;
    }

    last.next = temp;
    return root;
}

// Function to convert array to linked list
static Node arrayToList(int[] arr, int n) {
    Node root = null;
    for (int i = 0; i < n; i++) {
        root = insertEnd(root, arr[i]);
    }
    return root;
}

static void display(Node root) {
    while (root != null) {
        Console.Write(root.data);
        if (root.next != null)
            Console.Write(" -> ");
        root = root.next;
    }
}

static void Main(string[] args) {
    int[] arr = { 1, 2, 3, 4, 5 };
    int n = arr.Length;
    Node root = arrayToList(arr, n);
    display(root);
}

}

JavaScript

class Node { constructor(d) { this.data = d; this.next = null; } }

// Function to insert node at the end function insertEnd(root, item) { let temp = new Node(item); if (root === null) return temp;

let last = root;
while (last.next !== null) {
    last = last.next;
}

last.next = temp;
return root;

}

// Function to convert array to linked list function arrayToList(arr, n) { let root = null; for (let i = 0; i < n; i++) { root = insertEnd(root, arr[i]); } return root; }

function display(root) { while (root !== null) { process.stdout.write(root.data.toString()); if (root.next !== null) process.stdout.write(" -> "); root = root.next; } }

// Driver code let arr = [1, 2, 3, 4, 5]; let n = arr.length; let root = arrayToList(arr, n); display(root);

`

Output

1 -> 2 -> 3 -> 4 -> 5

**Time Complexity: O(n*n)
**Auxiliary Space: O(1)

[Expected Approach] Insertion at the beginning

C++ `

#include using namespace std;

// Representation of a node class Node { public: int data; Node* next;

Node(int d) {
    data = d;
    next = NULL;
}

};

// Function to insert node at the beginning void insert(Node** root, int item) { Node* temp = new Node(item); temp->next = *root; *root = temp; }

// Function to display linked list void display(Node* root) { while (root != NULL) { cout << root->data << " "; root = root->next; } }

// Convert array to linked list 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

class GFG {

// Representation of a node static class Node { int data; Node next; Node (int d) { data = d; next = null; } }; static Node root;

// Function to insert node static Node insert(Node root, int item){ Node temp = new Node(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); } }

Python

Representation of a Node

class Node: def init(self, data): self.data = data self.next = next

Function to insert Node

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 {

// Representation of a node public class Node { public int data; public Node next; }; static Node root;

// Function to insert node 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

// Representation of a node class Node { constructor(){ this.data = 0; this.next = null; } } var root;

// Function to insert node function insert(root, item){ var temp = new Node(); temp.data = item; temp.next = root; root = temp; return root; }

function display(root){ while (root != null) { console.log(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; }

// Driver code var arr = [ 1, 2, 3, 4, 5 ]; var n = arr.length; var root = arrayToList(arr, n); display(root);

`

**Time Complexity : O(n)
**Auxiliary Space : O(1)

Alternate Efficient Solution is maintain tail pointer, traverse array elements from left to right, insert at tail and update tail after insertion.