Question 4 ... You are asked to implement the following private and static metho
ID: 3724331 • Letter: Q
Question
Question 4 ... You are asked to implement the following private and static methods. These methods are added to the class SingleLinkedList. Unless otherwise stated, you may freely copy the list provided as argument. Also, you may use helper methods if you feel the need to .. use JAVA
Exercise 7 Node reverse (Node node) that reverses the given list. Provide two solutions. The first one returns a new list, the second reverses the given list (i.e. it does not create a copy of its elements) Exercise 8 NodeExplanation / Answer
If you post more than 1 question, as per chegg guidelines I have to solve only 1 question.
Exercise 8.
Node<Integer> doubleL(Node<Integer> node)
{
// if the given list is empty
if( node == null )
return null;
// create a new list
// initialized with the first node with double the value as in node
Node<E> new_head = new Node<E>(2 * node.getData());
// set the next pointer to null
new_head.setNext(null);
Node<E> new_trav = new_head;
// point trav to the second node
Node<E> trav = node.getNext();
// iterate through given list
while(trav != null)
{
// create a new node
Node<E> temp = new Node<E>( 2 * trav.getData() )
temp.setNext(null);
new_trav.setNext( temp );
new_trav = temp;
// move to next node
trav = trav.getNext();
}
return new_head;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.