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...
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...
Can you fix the code and comment the fix Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -39 at...
Can you fix the code and comment the fix Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -39 at CaesarCipher.encrypt(CaesarCipher.java:28) at CaesarCipher.main(CaesarCipher.java:52) public class CaesarCipher{     char[] encoder = new char[52];     char[] decoder = new char[52];      public CaesarCipher(int rotation)      {        for(int k=0 ; k < 26 ; k++)        {            encoder[k] = (char) ('A' + (k + rotation) % 26);            decoder[k] = (char) ('A' + (k - rotation + 26) % 26);        }        for(int j...
fix this code in python and show me the output. do not change the code import...
fix this code in python and show me the output. do not change the code import random #variables and constants MAX_ROLLS = 5 MAX_DICE_VAL = 6 #declare a list of roll types ROLLS_TYPES = [ "Junk" , "Pair" , "3 of a kind" , "5 of a kind" ] #set this to the value MAX_ROLLS pdice = [0,0,0,0,0] cdice = [0,0,0,0,0] #set this to the value MAX_DICE_VAL pdice = [0,0,0,0,0,0] cdice = [0,0,0,0,0,0] #INPUT - get the dice rolls i...
How can I fix this code to accomplish the goal of reading and writing on binary...
How can I fix this code to accomplish the goal of reading and writing on binary or text files? 3 import java.io.*; 4 import java.io.FileOutputStream; 5 import java.io.FileInputStream; 6 import java.util.Scanner; 7 8 public class ReadAndWrite implements Serializable 9 { 10 public static void main(String[] args) 11 { 12 boolean file = true; 13 Scanner inputStream; 14 PrintWriter outputStream; 15 FileInputStream inputBinary; 16 FileOutputStream readBinary; 17 FileInputStreamText writeText; 18 FIleOutputStreamText readText; 19 StringBuffer contents = new StringBuffer(); 20 Scanner keyboard...
I just wanted to double check my answers, can you just provide me with the correct...
I just wanted to double check my answers, can you just provide me with the correct answer for each one in order to cross-check, thank you. The United States' class system is heavily dependent upon an individual's social background. True False At which level of the factors which put women at risk does the following statement fit? "Tolerance of violence as a means of conflict resolution". Family/Relationship Level Individual Level Community Level Societal Level Which of the following countries is...
I just wanted to double check my answers, can you just provide me with the correct...
I just wanted to double check my answers, can you just provide me with the correct answer for each one in order to cross-check, thank you. When women carry the burden of poverty and are treated as non-equals when compared to men, which of the following terms describes this circumstance? Twice Burdened Double Discrimination Double Deprivation Double Jeopardy Which of the following countries was not a colony of Britain? Singapore Korea Hong Kong In 2012, which country utilized 2.8% of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT