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
Write a recursive method isOrdered() that takes a Node and twokeys min and max as arguments and returns true if all the keys in the tree are betweenmin and max; min and max are indeed the smallest and largest keys in the tree, respectively; and the BST ordering property holds for all keys in the tree; false otherwise.
Create a recursive method isOrdered() that takes a Node as an argument and two keys min and max as arguments and returns true if all the keys in the tree are between min and max; min and max are indeed the smallest and largest keys in the tree, respectively; and the BST ordering property holds for all keys in the tree; false otherwise.
Implement a recursive version of the size method for SinglyLinkedLists. (Hint: A wrapper may be useful.)
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