-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList_Recursive.java
More file actions
43 lines (40 loc) · 923 Bytes
/
Copy pathLinkedList_Recursive.java
File metadata and controls
43 lines (40 loc) · 923 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
// Recursive Java program to count number of nodes in
// a linked list
class Node
{
int data;
Node next;
Node(int d) { data = d; next = null; }
}
class LinkedList_Recursive
{
Node head;
public void push(int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
public int getCountRec(Node node)
{
if (node == null)
return 0;
return 1 + getCountRec(node.next);
}
public int getCount()
{
return getCountRec(head);
}
public static void main(String[] args)
{
LinkedList_Recursive lList = new LinkedList_Recursive();
lList.push(1);
lList.push(3);
lList.push(1);
lList.push(2);
lList.push(1);
lList.push(5);
System.out.println("Count of nodes is " +
lList.getCount());
}
}