Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
4th Edition
ISBN: 9780134787961
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
bartleby

Concept explainers

Expert Solution & Answer
Book Icon
Chapter 19, Problem 3AW

Explanation of Solution

Method definition for “reverse()”:

The recursive method definition for “reverse()” is given below:

/* Recursive method definition for "reverse" with parameter */

private Node reverse(Node list)

{

    //Create a node "newList"

    Node newList;

/* If the given node is null or last node is null, then */

    if((list == null) || (list.next == null))

    {

        /* Return the node */

        return list;

    }

/* Recursively call the method "reverse" for remaining node */

    newList = reverse(list.next);

    //Modify references for middle sequence

    list.next.next = list;

    list.next = null;

    //Return new head node in each recursion

    return newList;

}

/* Recursive Method definition for reverse method without parameter */

private void reverse()

{

    /* Call the method "reverse" with list head */

    first = reverse(first);

}

Explanation:

The above method definition is used to reverse the elements in a list.

  • Recursive method definition of “reverse()” with an parameter “list”.
    • Create a node “newList”.
    • If the given node is null or last node is null, then return the node.
    • Recursively call the method “reverse” for remaining node.
    • Modify references for middle sequence.
    • Finally, return new head node in each recursion.
  • Recursive method definition of “reverse()” without parameter.
    • Call the method “reverse” with list head.

Complete code:

The complete executable code for reverse the elements in a list using recursive “reverse()” method is given below:

//Define "LinkedList1" class

class LinkedList1

{

/** The code for this part is same as the textbook of "LinkedList1" class */  

/* Recursive method definition for "reverse" with parameter */

    private Node reverse(Node list)

    {

        //Create a node "newList"

        Node newList;

/* If the given node is null or last node is null, then */

        if((list == null) || (list.next == null))

        {

            /* Return the node */

            return list;

        }

/* Recursively call the method "reverse" for remaining node */

        newList = reverse(list...

Blurred answer
Students have asked these similar questions
AvgCompares(), a recursive function that calculates the average number of comparisons needed by a random search hit in a given BST (the internal path length of the tree divided by its size plus one), should be added to the BST. Create two implementations: a recursive method that adds a field to each node in the tree and takes linear space and constant time every query, and a method similar to size() that takes linear space and constant time per query.
This is recursive homework is being a bit tough please java code with comments.   Thanks
Write and implement a recursive version of the binary search algorithm. Also, write a version of the sequential search algorithm that can be applied to sorted lists. Add this operation to the class orderedArrayListType for array-based lists. Moreover, write a test program to test your algorithm.
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning