Convert a Singly Linked List to an array
Last Updated :
10 Jan, 2023
Given a singly linked list and the task is to convert it into an array.
Examples:
Input: List = 1 -> 2 -> 3 -> 4 -> 5 -> NULL
Output: 1 2 3 4 5
Input: List = 10 -> 20 -> 30 -> 40 -> 50 -> NULL
Output: 10 20 30 40 50
Approach: An approach to creating a linked list from the given array has been discussed in this article. Here, an approach to convert the given linked list to an array will be discussed.
- Find the length of the given linked list say len.
- Create an array of size len.
- Traverse the given linked list and store the elements in the array one at a time.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
struct node {
int data;
node* next;
};
node* add( int data)
{
node* newnode = new node;
newnode->data = data;
newnode->next = NULL;
return newnode;
}
void printArr( int a[], int n)
{
for ( int i = 0; i < n; i++)
cout << a[i] << " " ;
}
int findlength(node* head)
{
node* curr = head;
int cnt = 0;
while (curr != NULL) {
cnt++;
curr = curr->next;
}
return cnt;
}
void convertArr(node* head)
{
int len = findlength(head);
int arr[len];
int index = 0;
node* curr = head;
while (curr != NULL) {
arr[index++] = curr->data;
curr = curr->next;
}
printArr(arr, len);
}
int main()
{
node* head = NULL;
head = add(1);
head->next = add(2);
head->next->next = add(3);
head->next->next->next = add(4);
head->next->next->next->next = add(5);
convertArr(head);
return 0;
}
|
Java
class GFG
{
static class node
{
int data;
node next;
}
static node add( int data)
{
node newnode = new node();
newnode.data = data;
newnode.next = null ;
return newnode;
}
static void printArr( int a[], int n)
{
for ( int i = 0 ; i < n; i++)
System.out.print(a[i]+ " " );
}
static int findlength(node head)
{
node curr = head;
int cnt = 0 ;
while (curr != null )
{
cnt++;
curr = curr.next;
}
return cnt;
}
static void convertArr(node head)
{
int len = findlength(head);
int []arr = new int [len];
int index = 0 ;
node curr = head;
while (curr != null )
{
arr[index++] = curr.data;
curr = curr.next;
}
printArr(arr, len);
}
public static void main(String []args)
{
node head = new node();
head = add( 1 );
head.next = add( 2 );
head.next.next = add( 3 );
head.next.next.next = add( 4 );
head.next.next.next.next = add( 5 );
convertArr(head);
}
}
|
Python3
class node:
def __init__( self , data):
self .data = data
self . next = None
def add(data):
newnode = node( 0 )
newnode.data = data
newnode. next = None
return newnode
def printArr(a, n):
i = 0
while (i < n):
print (a[i], end = " " )
i = i + 1
def findlength( head):
curr = head
cnt = 0
while (curr ! = None ):
cnt = cnt + 1
curr = curr. next
return cnt
def convertArr(head):
len1 = findlength(head)
arr = []
index = 0
curr = head
while (curr ! = None ):
arr.append( curr.data)
curr = curr. next
printArr(arr, len1)
head = node( 0 )
head = add( 1 )
head. next = add( 2 )
head. next . next = add( 3 )
head. next . next . next = add( 4 )
head. next . next . next . next = add( 5 )
convertArr(head)
|
C#
using System;
class GFG
{
public class node
{
public int data;
public node next;
}
static node add( int data)
{
node newnode = new node();
newnode.data = data;
newnode.next = null ;
return newnode;
}
static void printArr( int []a, int n)
{
for ( int i = 0; i < n; i++)
Console.Write(a[i] + " " );
}
static int findlength(node head)
{
node curr = head;
int cnt = 0;
while (curr != null )
{
cnt++;
curr = curr.next;
}
return cnt;
}
static void convertArr(node head)
{
int len = findlength(head);
int []arr = new int [len];
int index = 0;
node curr = head;
while (curr != null )
{
arr[index++] = curr.data;
curr = curr.next;
}
printArr(arr, len);
}
public static void Main(String []args)
{
node head = new node();
head = add(1);
head.next = add(2);
head.next.next = add(3);
head.next.next.next = add(4);
head.next.next.next.next = add(5);
convertArr(head);
}
}
|
Javascript
<script>
class node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function add( data)
{
var newnode = new node();
newnode.data = data;
newnode.next = null ;
return newnode;
}
function printArr( a, n)
{
for (let i = 0; i < n; i++)
document.write(a[i]+ " " );
}
function findlength( head)
{
var curr = head;
let cnt = 0;
while (curr != null )
{
cnt++;
curr = curr.next;
}
return cnt;
}
function convertArr( head)
{
let len = findlength(head);
let arr = new Array(len);
let index = 0;
var curr = head;
while (curr != null )
{
arr[index++] = curr.data;
curr = curr.next;
}
printArr(arr, len);
}
var head = new node();
head = add(1);
head.next = add(2);
head.next.next = add(3);
head.next.next.next = add(4);
head.next.next.next.next = add(5);
convertArr(head);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...