Topic: Linked List
Questions Successfully Completed: 1
1) Convert Array to Linked List | Easy |
Convert Array to Linked List
Time Complexity : O(N)
Space Complexity: O(N)
package linkedlist;
class displayNode{
int data;
displayNode next;
displayNode(int data) {
this.data = data;
this.next = null;
}
}
public class displayLinkedList {
static displayNode create(int[] arr) {
displayNode first = new displayNode(arr[0]);
displayNode last = first;
for(int i=1;i<arr.length;i++){
displayNode sub = new displayNode(arr[i]);
last.next = sub;
last = sub;
}
return first;
}
static void display(int[] arr){
displayNode trav = create(arr);
while(trav!=null){
System.out.println(trav.data);
trav = trav.next;
}
}
public static void main(String[] args) {
int[] arr ={1,2,3,4};
create(arr);
display(arr);
}
}
// OUTPUT
//1
//2
//3
//4
Thank you for reading :)