-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplayRecursive.java
More file actions
37 lines (29 loc) · 929 Bytes
/
displayRecursive.java
File metadata and controls
37 lines (29 loc) · 929 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
//Question: Display a Linked List by using a recusive funtion.
package LinkedList;
public class displayRecursive {
public static class Node { // Making new class named "Node"
int val; // Has part (Data)
Node next; // Has part (Next node reference)
}
public static void displayRecursive(Node head) {
if (head == null) return;
System.out.println(head.val);
displayRecursive(head.next);
}
public static void main(String[] args) {
// Making 4 new "Node Objects" (a, b, c, d)
Node a = new Node();
Node b = new Node();
Node c = new Node();
Node d = new Node();
// Assigning values and linking
a.val = 10;
a.next = b;
b.val = 20;
b.next = c;
c.val = 30;
c.next = d;
d.val = 40;
displayRecursive(a);
}
}