Given a singly linked list and the task is to convert it into an array.
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 create 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:
1 2 3 4 5
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Convert singly linked list into circular linked list
- Convert a String to a Singly Linked List
- Convert Singly Linked List to XOR Linked List
- Difference between Singly linked list and Doubly linked list
- Sum of the nodes of a Singly Linked List
- QuickSort on Singly Linked List
- Circular Singly Linked List | Insertion
- Binary Search on Singly Linked List
- Alternate Odd and Even Nodes in a Singly Linked List
- Product of the nodes of a Singly Linked List
- Insertion Sort for Singly Linked List
- Alternating split of a given Singly Linked List | Set 1
- Implement a stack using singly linked list
- Find the common nodes in two singly linked list
- Sum and Product of all Prime Nodes of a Singly Linked List
- Sum and Product of the nodes of a Singly Linked List which are divisible by K
- Reverse a singly Linked List in groups of given size | Set 3
- Program to delete all even nodes from a Singly Linked List
- C Program to reverse each node value in Singly Linked List
- Select a Random Node from a Singly Linked List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.