Skip to content

Commit 4fe419e

Browse files
BamaCharanChhandogiBamaCharanChhandogi
and
BamaCharanChhandogi
authored
Add Recursive Reverse of Linked List (TheAlgorithms#4292)
Co-authored-by: BamaCharanChhandogi <b.c.chhandogi@gmailcom>
1 parent 1ef5208 commit 4fe419e

File tree

2 files changed

+69
-5
lines changed

2 files changed

+69
-5
lines changed

src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ public void swapNodes(int valueFirst, int valueSecond) {
119119
}
120120

121121
/**
122-
* Reverse a singly linked list from a given node till the end
122+
* Reverse a singly linked list[Iterative] from a given node till the end
123123
*
124124
*/
125-
public Node reverseList(Node node) {
125+
public Node reverseListIter(Node node) {
126126
Node prev = null;
127127
Node curr = node;
128128

@@ -141,6 +141,23 @@ public Node reverseList(Node node) {
141141
// the reversed linkedlist
142142
return prev;
143143
}
144+
/**
145+
* Reverse a singly linked list[Recursive] from a given node till the end
146+
*
147+
*/
148+
public Node reverseListRec(Node head) {
149+
if (head == null || head.next == null) {
150+
return head;
151+
}
152+
153+
Node prev = null;
154+
Node h2 = reverseListRec(head.next);
155+
156+
head.next.next = head;
157+
head.next = prev;
158+
159+
return h2;
160+
}
144161

145162
/**
146163
* Clear all nodes in the list

src/test/java/com/thealgorithms/datastructures/lists/SinglyLinkedListTest.java

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void reverseList() {
108108

109109
// Reversing the LinkedList using reverseList() method and storing the head of the reversed
110110
// 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());
112112

113113
// Recording the Nodes after reversing the LinkedList
114114
Node firstNode = head; // 4
@@ -133,7 +133,7 @@ void reverseListNullPointer() {
133133
Node first = list.getHead();
134134

135135
// Reversing the linkedlist
136-
Node head = list.reverseList(first);
136+
Node head = list.reverseListIter(first);
137137

138138
// checking whether the method works fine if the input is null
139139
assertEquals(head, first);
@@ -147,7 +147,7 @@ void reverseListTest() {
147147

148148
// Reversing the LinkedList using reverseList() method and storing the head of the reversed
149149
// linkedlist in a head node
150-
Node head = list.reverseList(list.getHead());
150+
Node head = list.reverseListIter(list.getHead());
151151

152152
// Storing the head in a temp variable, so that we cannot loose the track of head
153153
Node temp = head;
@@ -160,4 +160,51 @@ void reverseListTest() {
160160
i--;
161161
}
162162
}
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+
}
163210
}

0 commit comments

Comments
 (0)