Skip to content

Commit d4dec50

Browse files
committed
Unneccasry checks are removed
1 parent be5cba1 commit d4dec50

File tree

1 file changed

+2
-9
lines changed

1 file changed

+2
-9
lines changed

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,14 @@ public void swapNodes(int valueFirst, int valueSecond) {
123123
*
124124
*/
125125
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-
}
130126
Node prev = null;
131127
Node curr = node;
132-
Node next=curr.next;
128+
133129
while (curr != null) {
130+
Node next=curr.next;
134131
curr.next = prev;
135132
prev = curr;
136133
curr = next;
137-
//NullPointer Exception is handled here for next variable inside while loop
138-
if(next!=null){
139-
next = next.next;
140-
}
141134
}
142135
//prev will be pointing to the last element in the Linkedlist, it will be the new head of the reversed linkedlist
143136
return prev;

0 commit comments

Comments
 (0)