Skip to content

Commit c9fc75a

Browse files
committed
Minor changes in main and Junit tests implemented
1 parent db8f19b commit c9fc75a

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,11 @@ 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) {
125+
public Node reverseList(Node node) {
126+
//When node is null, then "next" will cause an error as we are accesing node.next, which will give a NullPointer Exception
127+
if(node==null){
128+
return node;
129+
}
126130
Node prev = null;
127131
Node curr = node;
128132
Node next=curr.next;
@@ -135,7 +139,7 @@ Node reverseList(Node node) {
135139
next = next.next;
136140
}
137141
}
138-
//prev will be pointing to the last element in the Linkedlist
142+
//prev will be pointing to the last element in the Linkedlist, it will be the new head of the linkedlist
139143
return prev;
140144
}
141145

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

Lines changed: 39 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,42 @@ 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+
SinglyLinkedList list=new SinglyLinkedList();
132+
Node first=list.getHead();
133+
134+
list.reverseList(first);
135+
136+
Node newFirst=list.getHead();
137+
assertEquals(first,newFirst);
138+
139+
}
140+
102141
}

0 commit comments

Comments
 (0)