Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java's question reverse the list in place... without creating any new nodes Here is the code is given: public class MyLinked { static class Node

Java's question

reverse the list "in place"... without creating any new nodes

Here is the code is given:

public class MyLinked {

static class Node {

public Node() { }

public double item;

public Node next;

}

int N;

Node first;

public MyLinked () {

first = null;

N = 0;

}

public void add (double item) {

Node newfirst = new Node ();

newfirst.item = item;

newfirst.next = first;

first = newfirst;

N++;

}

public void reverse () {

//Here need to do

}

private static void print (String s, MyLinked b) {

StdOut.print (s + ": ");

for (Node x = b.first; x != null; x = x.next)

StdOut.print (x.item + " ");

StdOut.println ();

}

private static void print (String s, MyLinked b, double i) {

StdOut.print (s + ": ");

for (Node x = b.first; x != null; x = x.next)

StdOut.print (x.item + " ");

StdOut.println (": " + i);

}

private static void testReverse () {

MyLinked b = new MyLinked ();

b.reverse ();

print ("reverse empty", b);

b.add (1);

print ("singleton", b);

b.reverse ();

print ("reverse singleton", b);

b.add (2);

print ("two", b);

b.reverse ();

print ("reverse two", b);

b.reverse ();

print ("reverse again", b);

for (double i = 3; i < 7; i++) {

b.add (i);

b.add (i);

}

print ("bigger list", b);

b.reverse ();

print ("reversed", b);

}

public static void main (String args[]) {

testReverse ();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Science For Dummies

Authors: Lillian Pierson ,Jake Porway

2nd Edition

1119327636, 978-1119327639

More Books

Students also viewed these Databases questions

Question

c. What were the reasons for their move? Did they come voluntarily?

Answered: 1 week ago

Question

5. How do economic situations affect intergroup relations?

Answered: 1 week ago