how to call function of linkedkist without using test case

Viewed 35

it is a linkedlist hackerrank question i want to call the reversewithoutextraspace function in main method without using testcase PLEASE help me

package Linked_List;

public class ReverseALinkedList {
    static ListNode reverseWithoutExtraSpace(ListNode head) {
        if (head == null) {
            return null;


        }
        if (head.next == null) {
            return null;
        }
        ListNode preNode = null;
        ListNode currNode = head;
        while (currNode != null) {
            ListNode nextNode = currNode.next;
            currNode.next = preNode;
            preNode = currNode;
            currNode = nextNode;

        }
        head = preNode;

        return head;
    }
}

This is the testing and herre i want main function calling PSVM which has reversewithout extraspace and not testing junit case

package Linked_List;

import Linked_List.ListNode;
import org.junit.jupiter.api.Test;

import static Linked_List.ReverseALinkedList.reverseWithoutExtraSpace;
import static org.junit.jupiter.api.Assertions.*;

public class ReverseALinkedListTest {
    private static final ReverseALinkedList reverseALinkedList = new ReverseALinkedList();

    public ReverseALinkedListTest() {
        reverseALinkedList = new ReverseALinkedList();
        //`enter code here`ListNode reversedList = reverseALinkedList.reverseWithoutExtraSpace(head);
    }

    @Test
    void testReverse1() {
        ListNode head = new ListNode(4);
        head.next = new ListNode(8);
        head.next.next = new ListNode(15);
        head.next.next.next = new ListNode(16);

        ListNode reversedList = reverseALinkedList.reverseWithoutExtraSpace(head);

        assertEquals(16, reversedList.val);
        assertEquals(15, reversedList.next.val);
        assertEquals(8, reversedList.next.next.val);
        assertEquals(4, reversedList.next.next.next.val);
        assertNull(reversedList.next.next.next.next);


    }

    


}

This is the Linked list Node and it has main method I know that

package Linked_List;


    public class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int x) {
            val = x;
        }

        


        }
    }
0 Answers
Related