@@ -108,7 +108,7 @@ void reverseList() {
108
108
109
109
// Reversing the LinkedList using reverseList() method and storing the head of the reversed
110
110
// linkedlist in a head node The reversed linkedlist will be 4->3->2->1->null
111
- Node head = list .reverseList (list .getHead ());
111
+ Node head = list .reverseListIter (list .getHead ());
112
112
113
113
// Recording the Nodes after reversing the LinkedList
114
114
Node firstNode = head ; // 4
@@ -133,7 +133,7 @@ void reverseListNullPointer() {
133
133
Node first = list .getHead ();
134
134
135
135
// Reversing the linkedlist
136
- Node head = list .reverseList (first );
136
+ Node head = list .reverseListIter (first );
137
137
138
138
// checking whether the method works fine if the input is null
139
139
assertEquals (head , first );
@@ -147,7 +147,7 @@ void reverseListTest() {
147
147
148
148
// Reversing the LinkedList using reverseList() method and storing the head of the reversed
149
149
// linkedlist in a head node
150
- Node head = list .reverseList (list .getHead ());
150
+ Node head = list .reverseListIter (list .getHead ());
151
151
152
152
// Storing the head in a temp variable, so that we cannot loose the track of head
153
153
Node temp = head ;
@@ -160,4 +160,51 @@ void reverseListTest() {
160
160
i --;
161
161
}
162
162
}
163
+ // This is Recursive Reverse List Test
164
+ // Test to check whether the method reverseListRec() works fine
165
+ void RecursiveReverseList () {
166
+ // Create a linked list: 1 -> 2 -> 3 -> 4 -> 5
167
+ SinglyLinkedList list = createSampleList (5 );
168
+
169
+ // Reversing the linked list using reverseList() method
170
+ Node head = list .reverseListRec (list .getHead ());
171
+
172
+ // Check if the reversed list is: 5 -> 4 -> 3 -> 2 -> 1
173
+ assertEquals (5 , head .value );
174
+ assertEquals (4 , head .next .value );
175
+ assertEquals (3 , head .next .next .value );
176
+ assertEquals (2 , head .next .next .next .value );
177
+ assertEquals (1 , head .next .next .next .next .value );
178
+ }
179
+
180
+ @ Test
181
+ void RecursiveReverseListNullPointer () {
182
+ // Create an empty linked list
183
+ SinglyLinkedList list = new SinglyLinkedList ();
184
+ Node first = list .getHead ();
185
+
186
+ // Reversing the empty linked list
187
+ Node head = list .reverseListRec (first );
188
+
189
+ // Check if the head remains the same (null)
190
+ assertNull (head );
191
+ }
192
+
193
+ @ Test
194
+ void RecursiveReverseListTest () {
195
+ // Create a linked list with values from 1 to 20
196
+ SinglyLinkedList list = createSampleList (20 );
197
+
198
+ // Reversing the linked list using reverseList() method
199
+ Node head = list .reverseListRec (list .getHead ());
200
+
201
+ // Check if the reversed list has the correct values
202
+ int i = 20 ;
203
+ Node temp = head ;
204
+ while (temp != null && i > 0 ) {
205
+ assertEquals (i , temp .value );
206
+ temp = temp .next ;
207
+ i --;
208
+ }
209
+ }
163
210
}
0 commit comments