Question: this is QUESTION : 1 Write a method that return SinglyLinkedList as reversed string For example: If the elements of a list is 1, 2,

this is QUESTION :

1 Write a method that return SinglyLinkedList as reversed string

For example:

If the elements of a list is

1, 2, 3, 4, 5, 6

the reverse string should be

6, 5, 4, 3, 2, 1

it should be printed in main

hint: a new list maybe a solution. use addFirst() or addLast()

implement reverse method

you have two steps:

1- creating a new linked list and all node of the first linked list to the new linked list in reverse order (you should use one of the two method addFirst() or addLast() to add in reverse

2- you should traverse the new list and add the element inside each node to string don't forget the space in the string. and there is no comma after last element

class Main { public static void main(String[] args) { //test your implmentation here SinglyLinkedList l=new SinglyLinkedList(); l.addFirst(6); l.addFirst(5); l.addFirst(4); l.addFirst(3); l.addFirst(2); l.addFirst(1); System.out.print(l.reverse()); } }

class SinglyLinkedList { private static class Node { private E element; // reference to the element stored at this node private Node next; // reference to the subsequent node in the list public Node(E e, Node n) { element = e; next = n; } public E getElement( ) { return element; } public Node getNext( ) { return next; } public void setNext(Node n) { next = n; } }

private Node head = null; // head node of the list (or null if empty) private Node tail = null; // last node of the list (or null if empty) private int size = 0; // number of nodes in the list public SinglyLinkedList( ) { } public int size( ) { return size; } public boolean isEmpty( ) { return size == 0; } public Node getHead( ) { // returns the head node if (isEmpty( )) return null; return head; }

public void addFirst(E e) { // adds element e to the front of the list head = new Node<>(e, head); // create and link a new node if (size == 0) tail = head; // special case: new node becomes tail also size++; } public void addLast(E e) { // adds element e to the end of the list Node newest = new Node<>(e, null); // node will eventually be the tail if (isEmpty()) head = newest; // special case: previously empty list else tail.setNext(newest); // new node after existing tail tail = newest; // new node becomes the tail size++; }

public E first() { // returns (but does not remove) the first element if (isEmpty()) return null; return head.getElement(); } public E last( ) { // returns (but does not remove) the last element if (isEmpty( )) return null; return tail.getElement( ); } //implment this Method public String reverse() { String s=""; SinglyLinkedList l2= new SinglyLinkedList(); Node c =head; //define a new list and add the element of the current list to it in revrse order //you should use one of the two method addFirst() or addLast() to add in reverse } //note that there are no comma after last elment. //loop the new linked list to store the element on s } return s; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!