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

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

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

Post 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 32 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 32 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 32 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("");
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply