Question

In: Computer Science

Can you fix to me this code plz I just want to print this method as...

Can you fix to me this code plz

I just want to print this method as the reverse. the problem is not printing reverse.

public void printBackward() {
      
       Node curr = head;
       Node prev = null;
       Node next = null;
      
       System.out.print("\nthe backward of the linkedlist is: ");
      
       while(curr != null) {

           next = curr.next;
           curr.next = prev;
           prev = curr;
           curr = next;
          
           System.out.print(" " + prev.data);
          
       }

Solutions

Expert Solution

public void printBackward(Node head) {
      
       Node curr = head;
       Node prev = null;
       Node next = null;
      
       System.out.print("\nthe backward of the linkedlist is: ");
      
       while(curr != null) {

           next = curr.next;
           curr.next = prev;
           prev = curr;
           curr = next;

}
           node = prev;
while (node != null) {
System.out.print(node.data + " ");
node = node.next;     
       }

// I fixed your code please check , if any doubt please comment

// give full program below

class LinkedList {
  
static Node head;
  
static class Node {
  
int data;
Node next;
  
Node(int d) {
data = d;
next = null;
}
}
  
/* Function to reverse the linked list */
public void printBackward(Node node) {
Node prev = null;
Node current = node;
Node next = null;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
node = prev;
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
}
  
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.head = new Node(85);
list.head.next = new Node(15);
list.head.next.next = new Node(4);
list.head.next.next.next = new Node(20); // list 85->15->4->20
list.printBackward(head);
}
}


Related Solutions

can you fix this problem plz: I want to return -1 if the element is not...
can you fix this problem plz: I want to return -1 if the element is not found. fix for me just that. thank you. public class Search{             public static void main(String[] args) {                                           Character [] anArray = {'a','b','c','d'};               Character st = 'a';                      iterativeSearch(anArray, st);           //System.out.println(iterativeSearch(anArray, st));    }    public static...
I already have the code of this program, I just want to know how to fix...
I already have the code of this program, I just want to know how to fix the code to Implement the 5th function (System.nanotime() and System.currentTimeMillis() methods) What Code does: Introduction Searching is a fundamental operation of computer applications and can be performed using either the inefficient linear search algorithm with a time complexity of O (n) or by using the more efficient binary search algorithm with a time complexity of O (log n). Task Requirements In this lab, you...
Can you solve and send me the original code which I can copy and paste plz....
Can you solve and send me the original code which I can copy and paste plz. Thank you. Kahn's Algorithm Implement Kahn's Algorithm for giving a topological ordering of a graph as discussed in class. The input graph will be in adjacency list format. 1. Count the in-degree (number of edges ending at) each vertex. 2. Create a queue of nodes with in-degree 0. 3. While the queue is not empty: a. Add the first element in the queue to...
In python I have my code written and I want just 4 functions to be fix...
In python I have my code written and I want just 4 functions to be fix in my code according to rule. My code is running but it has some problem regarding to rules. (I have did all the other things so you do not have to worry about other functions) 1) all the players has to play until he/she reaches to at least 500 points in first round. When user reach 500 points for the first time, user may...
can you please look at the following code and fix it for me so that it...
can you please look at the following code and fix it for me so that it does not have any syntax errors. also can you tell me what was fixed /** * Driver program to demonstrate calling methods of Client class. * * @author Doyt Perry/Tina Comston * @version Fall 2019 */ public class ClientDemo { public static void main() { /** * main method - makes this an executable program. */ // create a client with placeholder values System.out.println("Client...
Can someone tell me how to fix warning msg in my code of C ++? I...
Can someone tell me how to fix warning msg in my code of C ++? I got run-time error for this question please help me asap! Errors are: In function 'void bfs(int, int)': warning: comparison between signed and unsigned integer expressions [-Wsign-compare] for(int j = 0; j < adj[pppp].size(); j++){ ^ In function 'int main()': warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result] scanf("%d %d %d %d %d", &a, &q, &c, &N, &m); ^...
I need to fix this code, and could you please tell me what was the problem...
I need to fix this code, and could you please tell me what was the problem options 1 and 9 don't work #include <stdio.h> #include <time.h> #include <stdlib.h> // generate a random integer between lower and upper values int GenerateRandomInt(int lower, int upper){     int num =(rand()% (upper - lower+1))+lower;     return num; } // use random numbers to set the values of the matrix void InitializeMatrix(int row, int column, int dimension, int mat[][dimension]){     for(int i =0; i<row; i++){...
can you please create the code program in PYTHON for me. i want to create array...
can you please create the code program in PYTHON for me. i want to create array matrix Nx1 (N is multiple of 4 and start from 16), and matrix has the value of elements like this: if N = 16, matrix is [ 4 4 4 4 -4 -4 -4 -4 4 4 4 4 -4 -4 -4 -4] if N = 64, matrix is [8 8 8 8 8 8 8 8 -8 -8 -8 -8 -8 -8 -8...
I wrote this code and it produces a typeError, so please can you fix it? import...
I wrote this code and it produces a typeError, so please can you fix it? import random def first_to_a_word(): print("###### First to a Word ######") print("Instructions:") print("You will take turns choosing letters one at a time until a word is formed.") print("After each letter is chosen you will have a chance to confirm whether or not a word has been formed.") print("When a word is formed, the player who played the last letter wins!") print("One of you has been chosen...
I was wondering if you can tell me if the following code is correct and if...
I was wondering if you can tell me if the following code is correct and if its not can it be fixed so it does not have any syntax errors. Client one /** * Maintains information on an insurance client. * * @author Doyt Perry/<add your name here> * @version Fall 2019 */ public class Client { // instance variables private String lastName; private String firstName; private int age; private int height; private int weight; /** * First constructor for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT