Skip to content

NullPointer Exception is handled. #4142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Minor changes in main and Junit tests implemented
  • Loading branch information
Akshith121 committed Apr 8, 2023
commit c9fc75a3f06b5c9aa6ffda7464ab66f85cd2150f
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ public void swapNodes(int valueFirst, int valueSecond) {
* Reverse a singly linked list from a given node till the end
*
*/
Node reverseList(Node node) {
public Node reverseList(Node node) {
//When node is null, then "next" will cause an error as we are accesing node.next, which will give a NullPointer Exception
if(node==null){
return node;
}
Node prev = null;
Node curr = node;
Node next=curr.next;
Expand All @@ -135,7 +139,7 @@ Node reverseList(Node node) {
next = next.next;
}
}
//prev will be pointing to the last element in the Linkedlist
//prev will be pointing to the last element in the Linkedlist, it will be the new head of the linkedlist
return prev;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.thealgorithms.datastructures.lists;


import org.junit.jupiter.api.Test;

import java.util.ArrayList;
Expand Down Expand Up @@ -99,4 +100,42 @@ void deleteNth() {
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(){
SinglyLinkedList list=new SinglyLinkedList();
Node first=list.getHead();

list.reverseList(first);

Node newFirst=list.getHead();
assertEquals(first,newFirst);

}

}