forked from adityabisoi/ds-algo-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.java
67 lines (61 loc) · 2.32 KB
/
solution.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.Scanner;
public class PrintLinkedListInReverse {
//SinglyLinkedListNode class to represent a node
static class SinglyLinkedListNode {
public int data;
public SinglyLinkedListNode next;
public SinglyLinkedListNode(int nodeData) {
this.data = nodeData;
this.next = null;
}
}
//SinglyLinkedList class to represent a linked list
static class SinglyLinkedList {
public SinglyLinkedListNode head;
public SinglyLinkedListNode tail;
public SinglyLinkedList() {
this.head = null;
this.tail = null;
}
//Method to insert Node in the linked list
public void insertNode(int nodeData) {
SinglyLinkedListNode node = new SinglyLinkedListNode(nodeData);
if (this.head == null) {
this.head = node;
} else {
this.tail.next = node;
}
this.tail = node;
}
}
//Method to print Linked List in reverse order
//The below method makes use of recursion
static void reversePrint(SinglyLinkedListNode head) {
//Base case that checks if head is null
if(head == null)
return;
//Recursive call to method by pass next node
reversePrint(head.next);
//Finally printing out the data of current node
System.out.println(head.data);
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
//Input the number of test cases
int tests = scanner.nextInt();
//For each test case
for (int testsItr = 0; testsItr < tests; testsItr++) {
SinglyLinkedList llist = new SinglyLinkedList();
//Input the number of nodes in linked list
int llistCount = scanner.nextInt();
//Input the data for the nodes and insert them in the linked list
for (int i = 0; i < llistCount; i++) {
int llistItem = scanner.nextInt();
llist.insertNode(llistItem);
}
//Finally call the method to recursively print the linked list in reverse order
reversePrint(llist.head);
}
scanner.close();
}
}