Page 1 of 1

Assume we have a class, SinglyLinkedList (code below), and we would like to create a method within this class that we ca

Posted: Tue Jul 12, 2022 8:17 am
by answerhappygod
Assume We Have A Class Singlylinkedlist Code Below And We Would Like To Create A Method Within This Class That We Ca 1
Assume We Have A Class Singlylinkedlist Code Below And We Would Like To Create A Method Within This Class That We Ca 1 (10.85 KiB) Viewed 33 times
Assume We Have A Class Singlylinkedlist Code Below And We Would Like To Create A Method Within This Class That We Ca 2
Assume We Have A Class Singlylinkedlist Code Below And We Would Like To Create A Method Within This Class That We Ca 2 (16.18 KiB) Viewed 33 times
Assume We Have A Class Singlylinkedlist Code Below And We Would Like To Create A Method Within This Class That We Ca 3
Assume We Have A Class Singlylinkedlist Code Below And We Would Like To Create A Method Within This Class That We Ca 3 (18.65 KiB) Viewed 33 times
Please Write Full code and in Java
Assume we have a class, SinglyLinkedList (code below), and we would like to create a method within this class that we can call to reverse the ordering of the nodes (not just print their values in reverse order). In reference to the code below, write a method `public void reverse()' that reverses the ordering so that the linked list now starts at the tail, ends at the head, and each node now points to the node that was previously pointing to it.
class SinglyLinkedList { static class Node { private int data; private Node next; public Node(int data) { this.data = data; } public int data() { return data; } public Node next() { return next; } } private Node head; public SinglyLinkedList(Node head) { this.head = head; } // Java method to add an element to a linked list public void add(Node node) { Node current = head; while (current != null) { if (current.next == null) { current.next = node;
} } // Java method to add an element to a linked list public void add(Node node) { Node current = head; while (current != null) { } } if (current.next == null) { } } current.next = node; } // Java method to print a singly linked list public void print() { break; current = current.next; Node node = head; while (node != null) { System.out.print(node.data() + " "); node = node.next(); public void reverse() { // YOUR CODE HERE } System.out.println("");