Skip to content

Commit 5d44004

Browse files
committed
Modified SwapNodes(int , int) method
1 parent bb2a301 commit 5d44004

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,12 @@ public void insertNth(int data, int position) {
263263
public void swapNodes(int a, int b) {
264264
Node currentNode = head;
265265
Node prev_a , prev_b , a_node , b_node;
266-
while(currentNode.next.value!=a&&currentNode.next.value!=b){
266+
while(currentNode.next!=null&&currentNode.next.value!=a&&currentNode.next.value!=b){
267267
currentNode = currentNode.next;
268268
}
269+
if(currentNode.next==null){
270+
return;
271+
}
269272
if(currentNode.next.value==a){
270273
prev_a = currentNode;
271274
a_node = currentNode.next;

0 commit comments

Comments
 (0)