-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLLUsingArray.java
More file actions
48 lines (43 loc) · 1.04 KB
/
LLUsingArray.java
File metadata and controls
48 lines (43 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Node {
int data;
Node next;
Node(int val) {
data = val;
next = null;
}
}
public class LLUsingArray {
static Node createList(int[] arr) {
if(arr.length==0) {
return null;
}
Node head = new Node(arr[0]);
Node curr = head;
for(int i=1;i<arr.length;i++) {
curr.next = new Node(arr[i]);
curr = curr.next;
}
return head;
}
static void printList(Node head) {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " -> ");
temp = temp.next;
}
System.out.println("null");
}
static void travese(Node head) {
if(head == null) {
return;
}
System.out.print(head.data+"->");
travese(head.next);
}
public static void main(String args[]) {
int [] arr = {10,78,85,56,23,42};
Node head = createList(arr);
System.out.print("Linked List:");
travese(head);
}
}