Convert a Singly Linked List to an array Last Updated : 04 Sep, 2024 Comments Improve Suggest changes Like Article Like Report Given a singly linked list and the task is to convert it into an array.Examples: Input: List = 1 -> 2 -> 3 -> 4 -> NULL Output: 1 2 3 4Input: List = 10 -> 20 -> 30 -> 40 -> 50 -> NULL Output: 10 20 30 40 50 Approach:The idea is to iterate through the linked list and for each node add its value into the array. C++ // C++ code to convert a Singly Linked List to an array #include <bits/stdc++.h> using namespace std; class Node { public: int data; Node* next; Node(int data) { this->data = data; this->next = nullptr; } }; // Function to convert a singly linked list to an array vector<int> linkedListToArray(Node* head) { vector<int> arr; Node* curr = head; while (curr != nullptr) { arr.push_back(curr->data); curr = curr->next; } return arr; } void printArray(const vector<int>& arr) { for (int i = 0; i < arr.size(); i++) { cout << arr[i] << " "; } cout << endl; } int main() { // Create a hardcoded singly linked list: 1 -> 2 -> 3 -> 4 Node* head = new Node(1); head->next = new Node(2); head->next->next = new Node(3); head->next->next->next = new Node(4); vector<int> arr = linkedListToArray(head); printArray(arr); return 0; } C // C code to convert a Singly Linked List to an array #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; // Function to convert a singly linked list to an array void linkedListToArray(struct Node* head, int arr[], int* size) { int count = 0; struct Node* curr = head; // Traverse the linked list and fill the array while (curr != NULL) { arr[count] = curr->data; count++; curr = curr->next; } *size = count; } void printArray(int arr[], int size) { for (int i = 0; i < size; i++) { printf("%d ", arr[i]); } printf("\n"); } struct Node* createNode(int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = NULL; return newNode; } int main() { // Create a hardcoded singly linked list: 1 -> 2 -> 3 -> 4 struct Node* head = createNode(1); head->next = createNode(2); head->next->next = createNode(3); head->next->next->next = createNode(4); int arr[1000]; int size; linkedListToArray(head, arr, &size); printArray(arr, size); return 0; } Java // Java code to convert a Singly Linked List to an array import java.util.ArrayList; class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } class GfG { // Function to convert a singly linked list to an array static ArrayList<Integer> linkedListToArray(Node head) { ArrayList<Integer> arr = new ArrayList<>(); Node curr = head; while (curr != null) { arr.add(curr.data); curr = curr.next; } return arr; } static void printArray(ArrayList<Integer> arr) { for (int i = 0; i < arr.size(); i++) { System.out.print(arr.get(i) + " "); } System.out.println(); } public static void main(String[] args) { // Create a hardcoded singly linked list: 1 -> 2 -> // 3 -> 4 Node head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); head.next.next.next = new Node(4); ArrayList<Integer> arr = linkedListToArray(head); printArray(arr); } } Python # Python code to convert a Singly Linked List to an array class Node: def __init__(self, data): self.data = data self.next = None # Function to convert a singly linked list to an array def linked_list_to_array(head): arr = [] curr = head while curr is not None: arr.append(curr.data) curr = curr.next return arr # Function to print an array def print_array(arr): for i in arr: print(i, end=" ") print() if __name__ == "__main__": # Create a hardcoded singly linked list: 1 -> 2 -> 3 -> 4 head = Node(1) head.next = Node(2) head.next.next = Node(3) head.next.next.next = Node(4) arr = linked_list_to_array(head) print_array(arr) C# // C# code to convert a Singly Linked List to an array using System; using System.Collections.Generic; class Node { public int data; public Node next; public Node(int data) { this.data = data; this.next = null; } } class GfG { // Function to convert a singly linked list to an array public static List<int> LinkedListToArray(Node head) { List<int> arr = new List<int>(); Node curr = head; while (curr != null) { arr.Add(curr.data); curr = curr.next; } return arr; } public static void PrintArray(List<int> arr) { for (int i = 0; i < arr.Count; i++) { Console.Write(arr[i] + " "); } Console.WriteLine(); } static void Main() { // Create a hardcoded singly linked list: 1 -> 2 -> // 3 -> 4 Node head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); head.next.next.next = new Node(4); List<int> arr = LinkedListToArray(head); PrintArray(arr); } } JavaScript // Javascript code to convert a Singly Linked List to an // array class Node { constructor(data) { this.data = data; this.next = null; } } // Function to convert a singly linked list to an array function linkedListToArray(head) { const arr = []; let curr = head; while (curr !== null) { arr.push(curr.data); curr = curr.next; } return arr; } function printArray(arr) { for (let i = 0; i < arr.length; i++) { console.log(arr[i]); } } // Create a hardcoded singly linked list: 1 -> 2 -> 3 -> 4 const head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); head.next.next.next = new Node(4); const arr = linkedListToArray(head); printArray(arr); Output1 2 3 4 Time Complexity: O(n)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Convert a Singly Linked List to an array C code_freak Follow Improve Article Tags : Linked List Algorithms Technical Scripter DSA Arrays Technical Scripter 2019 +2 More Practice Tags : AlgorithmsArraysLinked List Similar Reads Convert a String to a Singly Linked List Given string str, the task is to convert it into a singly Linked List. Examples: Input: str = "ABCDABC" Output: A -> B -> C -> D -> A -> B -> C Input: str = "GEEKS" Output: G -> E -> E -> K -> S Approach: Create a Linked ListFetch each character of the string and insert 5 min read Convert an Array to a Circular Doubly Linked List Prerequisite: Doubly Linked list, Circular Linked List, Circular Doubly Linked ListGiven an array of N elements. The task is to write a program to convert the array into a circular doubly linked list. The idea is to start traversing the array and for every array element create a new list node and as 8 min read Convert Singly Linked List to XOR Linked List Prerequisite: XOR Linked List â A Memory Efficient Doubly Linked List | Set 1XOR Linked List â A Memory Efficient Doubly Linked List | Set 2 An XOR linked list is a memory efficient doubly linked list in which the next pointer of every node stores the XOR of previous and next node's address. Given a 9 min read Singly Linked List Tutorial A singly linked list is a fundamental data structure, it consists of nodes where each node contains a data field and a reference to the next node in the linked list. The next of the last node is null, indicating the end of the list. Linked Lists support efficient insertion and deletion operations.Un 8 min read Convert singly linked list into circular linked list Given a singly linked list, the task is to convert it into a circular linked list.Examples:Input: head: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULLOutput: Explanation: Singly linked list is converted to circular by pointing last node to head.Input: head: 2 -> 4 -> 6 -> 8 -> 10 - 11 min read Like