From db8f19b04853e20fb18cab84fdbf0a1588928414 Mon Sep 17 00:00:00 2001 From: Akshith121 Date: Tue, 4 Apr 2023 11:44:45 +0530 Subject: [PATCH 1/5] NullPointer Exception is handled --- .../datastructures/lists/SinglyLinkedList.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java index a4276b021002..6a9ff18ee8ca 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java @@ -123,19 +123,20 @@ public void swapNodes(int valueFirst, int valueSecond) { * */ Node reverseList(Node node) { - Node prevNode = head; - while (prevNode.next != node) { - prevNode = prevNode.next; - } - Node prev = null, curr = node, next; + Node prev = null; + Node curr = node; + Node next=curr.next; while (curr != null) { - next = curr.next; curr.next = prev; prev = curr; curr = next; + //NullPointer Exception is handled here + if(next!=null){ + next = next.next; + } } - prevNode.next = prev; - return head; + //prev will be pointing to the last element in the Linkedlist + return prev; } /** From c9fc75a3f06b5c9aa6ffda7464ab66f85cd2150f Mon Sep 17 00:00:00 2001 From: Akshith121 Date: Sat, 8 Apr 2023 22:40:57 +0530 Subject: [PATCH 2/5] Minor changes in main and Junit tests implemented --- .../lists/SinglyLinkedList.java | 8 +++- .../lists/SinglyLinkedListTest.java | 39 +++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java index 6a9ff18ee8ca..2c723483dc62 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java @@ -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; @@ -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; } diff --git a/src/test/java/com/thealgorithms/datastructures/lists/SinglyLinkedListTest.java b/src/test/java/com/thealgorithms/datastructures/lists/SinglyLinkedListTest.java index b02fb433ad4a..70d0f13815f2 100644 --- a/src/test/java/com/thealgorithms/datastructures/lists/SinglyLinkedListTest.java +++ b/src/test/java/com/thealgorithms/datastructures/lists/SinglyLinkedListTest.java @@ -1,5 +1,6 @@ package com.thealgorithms.datastructures.lists; + import org.junit.jupiter.api.Test; import java.util.ArrayList; @@ -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); + + } + } \ No newline at end of file From be5cba1b365abefb1ac0a4dc56ed85993a1c3a70 Mon Sep 17 00:00:00 2001 From: Akshith121 Date: Sat, 8 Apr 2023 23:10:10 +0530 Subject: [PATCH 3/5] Junit tests --- .../lists/SinglyLinkedList.java | 4 +-- .../lists/SinglyLinkedListTest.java | 28 +++++++++++++++++-- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java index 2c723483dc62..8e28912c5342 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java @@ -134,12 +134,12 @@ public Node reverseList(Node node) { curr.next = prev; prev = curr; curr = next; - //NullPointer Exception is handled here + //NullPointer Exception is handled here for next variable inside while loop if(next!=null){ next = next.next; } } - //prev will be pointing to the last element in the Linkedlist, it will be the new head of the linkedlist + //prev will be pointing to the last element in the Linkedlist, it will be the new head of the reversed linkedlist return prev; } diff --git a/src/test/java/com/thealgorithms/datastructures/lists/SinglyLinkedListTest.java b/src/test/java/com/thealgorithms/datastructures/lists/SinglyLinkedListTest.java index 70d0f13815f2..1a537bd33c3a 100644 --- a/src/test/java/com/thealgorithms/datastructures/lists/SinglyLinkedListTest.java +++ b/src/test/java/com/thealgorithms/datastructures/lists/SinglyLinkedListTest.java @@ -128,14 +128,36 @@ void reverseList(){ //Test to check whether implemented reverseList() method handles NullPointer Exception for TestCase where head==null @Test void reverseListNullPointer(){ + //Creating a linkedlist with first node assigned to null SinglyLinkedList list=new SinglyLinkedList(); Node first=list.getHead(); + + //Reversing the linkedlist + Node head=list.reverseList(first); - list.reverseList(first); + //checking whether the method works fine if the input is null + assertEquals(head,first); + } - Node newFirst=list.getHead(); - assertEquals(first,newFirst); + //Testing reverseList() method for a linkedlist of size: 20 + @Test + void reverseListTest(){ + //Creating a new linkedlist + SinglyLinkedList list = createSampleList(20); + //Reversing the LinkedList using reverseList() method and storing the head of the reversed linkedlist in a head node + Node head=list.reverseList(list.getHead()); + + //Storing the head in a temp variable, so that we cannot loose the track of head + Node temp=head; + + int i=20; //This is for the comparison of values of nodes of the reversed linkedlist + //Checking whether the reverseList() method performed its task + while(temp!=null && i>0){ + assertEquals(temp.value,i); + temp=temp.next; + i--; + } } } \ No newline at end of file From d4dec50535eccb93b51c01aeaf2c57dbe71b20bf Mon Sep 17 00:00:00 2001 From: Akshith121 Date: Wed, 12 Apr 2023 12:36:56 +0530 Subject: [PATCH 4/5] Unneccasry checks are removed --- .../datastructures/lists/SinglyLinkedList.java | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java index 8e28912c5342..b4ca778be94c 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java @@ -123,21 +123,14 @@ public void swapNodes(int valueFirst, int valueSecond) { * */ 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; + while (curr != null) { + Node next=curr.next; curr.next = prev; prev = curr; curr = next; - //NullPointer Exception is handled here for next variable inside while loop - if(next!=null){ - next = next.next; - } } //prev will be pointing to the last element in the Linkedlist, it will be the new head of the reversed linkedlist return prev; From cee806d446f9cb9896a3a92d5800bb5cf4e319b0 Mon Sep 17 00:00:00 2001 From: Akshith121 Date: Sat, 15 Apr 2023 11:05:19 +0530 Subject: [PATCH 5/5] Changes were done and checked Junit testcases --- .../datastructures/lists/SinglyLinkedList.java | 7 ++++++- .../datastructures/lists/SinglyLinkedListTest.java | 6 +++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java index b4ca778be94c..df460938390a 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java @@ -126,12 +126,17 @@ public Node reverseList(Node node) { Node prev = null; Node curr = node; - while (curr != null) { + while (curr != null && curr.next != null) { Node next=curr.next; curr.next = prev; prev = curr; curr = next; } + //when curr.next==null, the current element is left without pointing it to its prev,so + if(curr != null){ + curr.next = prev; + prev=curr; + } //prev will be pointing to the last element in the Linkedlist, it will be the new head of the reversed linkedlist return prev; } diff --git a/src/test/java/com/thealgorithms/datastructures/lists/SinglyLinkedListTest.java b/src/test/java/com/thealgorithms/datastructures/lists/SinglyLinkedListTest.java index 1a537bd33c3a..94896132cb36 100644 --- a/src/test/java/com/thealgorithms/datastructures/lists/SinglyLinkedListTest.java +++ b/src/test/java/com/thealgorithms/datastructures/lists/SinglyLinkedListTest.java @@ -154,9 +154,9 @@ void reverseListTest(){ int i=20; //This is for the comparison of values of nodes of the reversed linkedlist //Checking whether the reverseList() method performed its task while(temp!=null && i>0){ - assertEquals(temp.value,i); - temp=temp.next; - i--; + assertEquals(i,temp.value); + temp=temp.next; + i--; } }