-
Notifications
You must be signed in to change notification settings - Fork 0
/
traversingLL.java
45 lines (43 loc) · 1.12 KB
/
traversingLL.java
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
class Node{
int data;
Node next;
Node(int data1, Node next1){
this.data=data1;
this.next=next1;
}
Node(int data1){
this.data = data1;
this.next=null;
}
}
public class traversingLL {
private static Node convertArr2LL(int [] arr){
Node head = new Node(arr[0]);
Node mover = head;
for (int i = 1; i < arr.length; i++) {
Node temp = new Node(arr[i]);
mover.next=temp;
mover = temp;
}
return head;
}
// private static void print(Node head){
// while (head!= null) {
// System.out.print(head.data+" ");
// head = head.next;
// }
// System.out.println();
// }
public static void main(String[] args) {
int arr[] = {2,3,4,5};
Node head = convertArr2LL(arr);
int length = 0;
//traversing a linkedList
while (head!=null) {
System.out.print(head.data+" ");
head = head.next;
length++;
}
System.out.println("\nThe length of the linked list is "+length);
}
}