Create linked list from a given array
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; // Representation of a node struct Node { int data; Node* next; }; // Function to insert node 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; } // Driver code 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
// Java implementation of the approach import java.util.*; class GFG { // Representation of a node static class Node { int data; Node next; }; // Function to insert node 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; } // Driver code public static void main(String args[]) { int arr[] = { 1, 2, 3, 4, 5 }; int n = arr.length; Node root = arrayToList(arr, n); display(root); } } // This code is contributed by Arnab Kundu |
C#
// C# implementation of the above approach using System; class GFG { // Representation of a node public class Node { public int data; public Node next; }; // Function to insert node 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; } // Driver code public static void Main(String []args) { int []arr = { 1, 2, 3, 4, 5 }; int n = arr.Length; Node root = arrayToList(arr, n); display(root); } } // This code is contributed by PrinciRaj1992 |
1 2 3 4 5
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; // Representation of a node struct Node { int data; Node* next; }; // Function to insert node 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; } // Driver code 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
// Java program to print level order traversal // in spiral form using one queue and one stack. import java.util.*; class GFG { // Representation of a node static class Node { int data; 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) { 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; } // Driver code public static void main(String[] args) { int arr[] = { 1, 2, 3, 4, 5 }; int n = arr.length; Node root = arrayToList(arr, n); display(root); } } // This code is contributed by Princi Singh |
C#
// C# program to print level order traversal // in spiral form using one queue and one stack. 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; } // Driver code public static void Main(String[] args) { int []arr = { 1, 2, 3, 4, 5 }; int n = arr.Length; Node root = arrayToList(arr, n); display(root); } } // This code is contributed by Rajput-Ji |
1 2 3 4 5
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.
Recommended Posts:
- Create new linked list from two given linked list with greater element at each node
- Create a linked list from two linked lists by choosing max element at each position
- Create a Doubly Linked List from a Ternary Tree
- Linked List vs Array
- A Programmer's approach of looking at Array vs. Linked List
- Replace even nodes of a doubly linked list with the elements of array
- Sort the linked list in the order of elements appearing in the array
- Difference between Singly linked list and Doubly linked list
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- Merge a linked list into another linked list at alternate positions
- Convert singly linked list into circular linked list
- Convert Singly Linked List to XOR Linked List
- Check if a linked list is Circular Linked List
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



