Given a Linked List in Java, the task is to convert this LinkedList to Array.
Input: LinkedList: ['G', 'e', 'e', 'k', 's'] Output: Array: ['G', 'e', 'e', 'k', 's'] Input: LinkedList: [1, 2, 3, 4, 5] Output: Array: [1, 2, 3, 4, 5]
- Get the LinkedList
- Convert the LinkedList to Object array using toArray() method.
- Convert the Object array to desired type array using Arrays.copyOf() method.
- Return the print the Array
Below is the implementation of the above approach:
Linked list: [G, e, e, k, s] Array: [G, e, e, k, s]
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.