mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-09 12:11:28 +08:00
Add Recursive Reverse of Linked List (#4292)
Co-authored-by: BamaCharanChhandogi <b.c.chhandogi@gmailcom>
This commit is contained in:

committed by
GitHub

parent
1ef5208b75
commit
4fe419ebd8
@ -119,10 +119,10 @@ public class SinglyLinkedList extends Node {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reverse a singly linked list from a given node till the end
|
* Reverse a singly linked list[Iterative] from a given node till the end
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public Node reverseList(Node node) {
|
public Node reverseListIter(Node node) {
|
||||||
Node prev = null;
|
Node prev = null;
|
||||||
Node curr = node;
|
Node curr = node;
|
||||||
|
|
||||||
@ -141,6 +141,23 @@ public class SinglyLinkedList extends Node {
|
|||||||
// the reversed linkedlist
|
// the reversed linkedlist
|
||||||
return prev;
|
return prev;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Reverse a singly linked list[Recursive] from a given node till the end
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public Node reverseListRec(Node head) {
|
||||||
|
if (head == null || head.next == null) {
|
||||||
|
return head;
|
||||||
|
}
|
||||||
|
|
||||||
|
Node prev = null;
|
||||||
|
Node h2 = reverseListRec(head.next);
|
||||||
|
|
||||||
|
head.next.next = head;
|
||||||
|
head.next = prev;
|
||||||
|
|
||||||
|
return h2;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clear all nodes in the list
|
* Clear all nodes in the list
|
||||||
|
@ -108,7 +108,7 @@ public class SinglyLinkedListTest {
|
|||||||
|
|
||||||
// Reversing the LinkedList using reverseList() method and storing the head of the reversed
|
// Reversing the LinkedList using reverseList() method and storing the head of the reversed
|
||||||
// linkedlist in a head node The reversed linkedlist will be 4->3->2->1->null
|
// linkedlist in a head node The reversed linkedlist will be 4->3->2->1->null
|
||||||
Node head = list.reverseList(list.getHead());
|
Node head = list.reverseListIter(list.getHead());
|
||||||
|
|
||||||
// Recording the Nodes after reversing the LinkedList
|
// Recording the Nodes after reversing the LinkedList
|
||||||
Node firstNode = head; // 4
|
Node firstNode = head; // 4
|
||||||
@ -133,7 +133,7 @@ public class SinglyLinkedListTest {
|
|||||||
Node first = list.getHead();
|
Node first = list.getHead();
|
||||||
|
|
||||||
// Reversing the linkedlist
|
// Reversing the linkedlist
|
||||||
Node head = list.reverseList(first);
|
Node head = list.reverseListIter(first);
|
||||||
|
|
||||||
// checking whether the method works fine if the input is null
|
// checking whether the method works fine if the input is null
|
||||||
assertEquals(head, first);
|
assertEquals(head, first);
|
||||||
@ -147,7 +147,7 @@ public class SinglyLinkedListTest {
|
|||||||
|
|
||||||
// Reversing the LinkedList using reverseList() method and storing the head of the reversed
|
// Reversing the LinkedList using reverseList() method and storing the head of the reversed
|
||||||
// linkedlist in a head node
|
// linkedlist in a head node
|
||||||
Node head = list.reverseList(list.getHead());
|
Node head = list.reverseListIter(list.getHead());
|
||||||
|
|
||||||
// Storing the head in a temp variable, so that we cannot loose the track of head
|
// Storing the head in a temp variable, so that we cannot loose the track of head
|
||||||
Node temp = head;
|
Node temp = head;
|
||||||
@ -160,4 +160,51 @@ public class SinglyLinkedListTest {
|
|||||||
i--;
|
i--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// This is Recursive Reverse List Test
|
||||||
|
// Test to check whether the method reverseListRec() works fine
|
||||||
|
void RecursiveReverseList() {
|
||||||
|
// Create a linked list: 1 -> 2 -> 3 -> 4 -> 5
|
||||||
|
SinglyLinkedList list = createSampleList(5);
|
||||||
|
|
||||||
|
// Reversing the linked list using reverseList() method
|
||||||
|
Node head = list.reverseListRec(list.getHead());
|
||||||
|
|
||||||
|
// Check if the reversed list is: 5 -> 4 -> 3 -> 2 -> 1
|
||||||
|
assertEquals(5, head.value);
|
||||||
|
assertEquals(4, head.next.value);
|
||||||
|
assertEquals(3, head.next.next.value);
|
||||||
|
assertEquals(2, head.next.next.next.value);
|
||||||
|
assertEquals(1, head.next.next.next.next.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void RecursiveReverseListNullPointer() {
|
||||||
|
// Create an empty linked list
|
||||||
|
SinglyLinkedList list = new SinglyLinkedList();
|
||||||
|
Node first = list.getHead();
|
||||||
|
|
||||||
|
// Reversing the empty linked list
|
||||||
|
Node head = list.reverseListRec(first);
|
||||||
|
|
||||||
|
// Check if the head remains the same (null)
|
||||||
|
assertNull(head);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void RecursiveReverseListTest() {
|
||||||
|
// Create a linked list with values from 1 to 20
|
||||||
|
SinglyLinkedList list = createSampleList(20);
|
||||||
|
|
||||||
|
// Reversing the linked list using reverseList() method
|
||||||
|
Node head = list.reverseListRec(list.getHead());
|
||||||
|
|
||||||
|
// Check if the reversed list has the correct values
|
||||||
|
int i = 20;
|
||||||
|
Node temp = head;
|
||||||
|
while (temp != null && i > 0) {
|
||||||
|
assertEquals(i, temp.value);
|
||||||
|
temp = temp.next;
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user