-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElements_LL.java
More file actions
43 lines (40 loc) · 872 Bytes
/
Copy pathElements_LL.java
File metadata and controls
43 lines (40 loc) · 872 Bytes
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
// Counting the Number of Elements in a Linked List.
//Iterative Solution
class Node
{
int data;
Node next;
Node(int d) { data = d; next = null; }
}
class Elements_LL
{
Node head;
public void push(int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
public int getCount()
{
Node temp = head;
int count = 0;
while (temp != null)
{
count++;
temp = temp.next;
}
return count;
}
public static void main(String[] args)
{
Elements_LL llist = new Elements_LL();
llist.push(1);
llist.push(3);
llist.push(1);
llist.push(2);
llist.push(1);
System.out.println("Count of nodes is " +
llist.getCount());
}
}