Skip to content

Commit 1ce9076

Browse files
authored
Fix NullPointer Exception (TheAlgorithms#4142)
1 parent 0c618b5 commit 1ce9076

File tree

2 files changed

+74
-10
lines changed

2 files changed

+74
-10
lines changed

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,20 +122,23 @@ public void swapNodes(int valueFirst, int valueSecond) {
122122
* Reverse a singly linked list from a given node till the end
123123
*
124124
*/
125-
Node reverseList(Node node) {
126-
Node prevNode = head;
127-
while (prevNode.next != node) {
128-
prevNode = prevNode.next;
129-
}
130-
Node prev = null, curr = node, next;
131-
while (curr != null) {
132-
next = curr.next;
125+
public Node reverseList(Node node) {
126+
Node prev = null;
127+
Node curr = node;
128+
129+
while (curr != null && curr.next != null) {
130+
Node next=curr.next;
133131
curr.next = prev;
134132
prev = curr;
135133
curr = next;
136134
}
137-
prevNode.next = prev;
138-
return head;
135+
//when curr.next==null, the current element is left without pointing it to its prev,so
136+
if(curr != null){
137+
curr.next = prev;
138+
prev=curr;
139+
}
140+
//prev will be pointing to the last element in the Linkedlist, it will be the new head of the reversed linkedlist
141+
return prev;
139142
}
140143

141144
/**

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.thealgorithms.datastructures.lists;
22

3+
34
import org.junit.jupiter.api.Test;
45

56
import java.util.ArrayList;
@@ -99,4 +100,64 @@ void deleteNth() {
99100
list.deleteNth(6); //Index 6 has value 7
100101
assertFalse(list.search(7));
101102
}
103+
//Test to check whether the method reverseList() works fine
104+
@Test
105+
void reverseList(){
106+
107+
//Creating a new LinkedList of size:4
108+
//The linkedlist will be 1->2->3->4->null
109+
SinglyLinkedList list = createSampleList(4);
110+
111+
//Reversing the LinkedList using reverseList() method and storing the head of the reversed linkedlist in a head node
112+
//The reversed linkedlist will be 4->3->2->1->null
113+
Node head=list.reverseList(list.getHead());
114+
115+
//Recording the Nodes after reversing the LinkedList
116+
Node firstNode = head; //4
117+
Node secondNode = firstNode.next; //3
118+
Node thirdNode = secondNode.next; //2
119+
Node fourthNode = thirdNode.next; //1
120+
121+
//Checking whether the LinkedList is reversed or not by comparing the original list and reversed list nodes
122+
assertEquals(1,fourthNode.value);
123+
assertEquals(2,thirdNode.value);
124+
assertEquals(3,secondNode.value);
125+
assertEquals(4,firstNode.value);
126+
}
127+
128+
//Test to check whether implemented reverseList() method handles NullPointer Exception for TestCase where head==null
129+
@Test
130+
void reverseListNullPointer(){
131+
//Creating a linkedlist with first node assigned to null
132+
SinglyLinkedList list=new SinglyLinkedList();
133+
Node first=list.getHead();
134+
135+
//Reversing the linkedlist
136+
Node head=list.reverseList(first);
137+
138+
//checking whether the method works fine if the input is null
139+
assertEquals(head,first);
140+
}
141+
142+
//Testing reverseList() method for a linkedlist of size: 20
143+
@Test
144+
void reverseListTest(){
145+
//Creating a new linkedlist
146+
SinglyLinkedList list = createSampleList(20);
147+
148+
//Reversing the LinkedList using reverseList() method and storing the head of the reversed linkedlist in a head node
149+
Node head=list.reverseList(list.getHead());
150+
151+
//Storing the head in a temp variable, so that we cannot loose the track of head
152+
Node temp=head;
153+
154+
int i=20; //This is for the comparison of values of nodes of the reversed linkedlist
155+
//Checking whether the reverseList() method performed its task
156+
while(temp!=null && i>0){
157+
assertEquals(i,temp.value);
158+
temp=temp.next;
159+
i--;
160+
}
161+
}
162+
102163
}

0 commit comments

Comments
 (0)