- Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathLengthOfLinkedList.java
52 lines (37 loc) · 1.18 KB
/
LengthOfLinkedList.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
46
47
48
49
50
51
52
packageLinkedList;
classNode<T> {
publicTdata;
publicLinkedList.Node<T> next;
publicNode(Tdata) {
this.data = data;
next = null;
}
}
publicclassLengthOfLinkedList {
publicstaticLinkedList.Node<Integer> createLinkedList() {
LinkedList.Node<Integer> n1 = newLinkedList.Node<>(10);
LinkedList.Node<Integer> n2 = newLinkedList.Node<>(20);
LinkedList.Node<Integer> n3 = newLinkedList.Node<>(30);
LinkedList.Node<Integer> n4 = newLinkedList.Node<>(40);
LinkedList.Node<Integer> n5 = newLinkedList.Node<>(50);
n1.next = n2;
n2.next = n3;
n3.next = n4;
n4.next = n5;
returnn1;
}
publicstaticintlength1(LinkedList.Node<Integer> head) {
intcount = 0;
LinkedList.Node<Integer> temp = head;
while (temp != null) {
count++;
temp = temp.next;
}
returncount;
}
publicstaticvoidmain(String[] args) {
LinkedList.Node<Integer> head = createLinkedList();
intans = length1(head);
System.out.println(ans);
}
}