Repository: TheAlgorithms/Java
Pull request #4142 (2023-04-04 06:52:49)

NullPointer Exception is handled.
<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.


Akshith121: > The code looks good, could you please add some JUnit tests to prove the fix is effective? (see https://github.com/TheAlgorithms/Java/tree/master/src/test/java/com/thealgorithms)

I have made some minor changes to reverseList() method and added some JUnit tests to prove that code works fine in any testcase

Patch:
diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java
index a4276b02..df460938 100644
--- a/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java
+++ b/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java
@@ -122,20 +122,23 @@ public class SinglyLinkedList extends Node {
      * Reverse a singly linked list from a given node till the end
      *
      */
-    Node reverseList(Node node) {
-        Node prevNode = head;
-        while (prevNode.next != node) {
-            prevNode = prevNode.next;
-        }
-        Node prev = null, curr = node, next;
-        while (curr != null) {
-            next = curr.next;
+    public Node reverseList(Node node) {
+        Node prev = null;
+        Node curr = node;
+        
+        while (curr != null && curr.next != null) {
+            Node next=curr.next;
             curr.next = prev;
             prev = curr;
             curr = next;
         }
-        prevNode.next = prev;
-        return head;
+        //when curr.next==null, the current element is left without pointing it to its prev,so 
+        if(curr != null){
+            curr.next = prev;
+            prev=curr;
+        }
+        //prev will be pointing to the last element in the Linkedlist, it will be the new head of the reversed linkedlist
+        return prev;
     }
 
     /**


Test patch:
diff --git a/src/test/java/com/thealgorithms/datastructures/lists/SinglyLinkedListTest.java b/src/test/java/com/thealgorithms/datastructures/lists/SinglyLinkedListTest.java
index b02fb433..94896132 100644
--- a/src/test/java/com/thealgorithms/datastructures/lists/SinglyLinkedListTest.java
+++ b/src/test/java/com/thealgorithms/datastructures/lists/SinglyLinkedListTest.java
@@ -1,5 +1,6 @@
 package com.thealgorithms.datastructures.lists;
 
+
 import org.junit.jupiter.api.Test;
 
 import java.util.ArrayList;
@@ -99,4 +100,64 @@ public class SinglyLinkedListTest {
         list.deleteNth(6); //Index 6 has value 7
         assertFalse(list.search(7));
     }
+    //Test to check whether the method reverseList() works fine
+    @Test
+    void reverseList(){
+
+        //Creating a new LinkedList of size:4
+        //The linkedlist will be 1->2->3->4->null
+        SinglyLinkedList list = createSampleList(4);
+          
+        //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
+        Node head=list.reverseList(list.getHead());
+
+        //Recording the Nodes after reversing the LinkedList
+        Node firstNode = head;              //4
+        Node secondNode = firstNode.next;   //3
+        Node thirdNode = secondNode.next;   //2
+        Node fourthNode = thirdNode.next;   //1
+        
+        //Checking whether the LinkedList is reversed or not by comparing the original list and reversed list nodes
+        assertEquals(1,fourthNode.value);
+        assertEquals(2,thirdNode.value);
+        assertEquals(3,secondNode.value);
+        assertEquals(4,firstNode.value);
+    }
+    
+    //Test to check whether implemented reverseList() method handles NullPointer Exception for TestCase where head==null
+    @Test
+    void reverseListNullPointer(){
+        //Creating a linkedlist with first node assigned to null
+        SinglyLinkedList list=new SinglyLinkedList();
+        Node first=list.getHead();
+        
+        //Reversing the linkedlist
+        Node head=list.reverseList(first);
+
+        //checking whether the method works fine if the input is null
+        assertEquals(head,first);
+    }
+
+    //Testing reverseList() method for a linkedlist of size: 20
+    @Test
+    void reverseListTest(){
+        //Creating a new linkedlist
+        SinglyLinkedList list = createSampleList(20);
+
+        //Reversing the LinkedList using reverseList() method and storing the head of the reversed linkedlist in a head node
+        Node head=list.reverseList(list.getHead());
+        
+        //Storing the head in a temp variable, so that we cannot loose the track of head
+        Node temp=head;
+
+        int i=20; //This is for the comparison of values of nodes of the reversed linkedlist
+        //Checking whether the reverseList() method performed its task
+        while(temp!=null && i>0){
+             assertEquals(i,temp.value);
+             temp=temp.next;
+             i--;
+        }
+    }
+
 }
\ No newline at end of file