Click here to Skip to main content
15,889,874 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
,i want to call the function of linkedlist in tescase funtion but using psvm and not using testcase

What I have tried:

this is the function of reverselinked list
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 test 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();
        //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);


    }

    
    }


}

Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900