Advertisement
korobushk

print in reverse ll

May 2nd, 2021 (edited)
1,310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.93 KB | None | 0 0
  1. import java.io.*;
  2. import java.math.*;
  3. import java.security.*;
  4. import java.text.*;
  5. import java.util.*;
  6. import java.util.concurrent.*;
  7. import java.util.regex.*;
  8.  
  9.  
  10.  
  11.     // Complete the reversePrint function below.
  12.  
  13.     /*
  14.      * For your reference:
  15.      *
  16.      * SinglyLinkedListNode {
  17.      *     int data;
  18.      *     SinglyLinkedListNode next;
  19.      * }
  20.      *
  21.      */
  22.     static void reversePrint(SinglyLinkedListNode head) {
  23.         SinglyLinkedListNode foward = null;
  24.         SinglyLinkedListNode prev = null;
  25.         SinglyLinkedListNode current = head;
  26.         while(current!=null){
  27.             foward = current.next;
  28.             current.next = prev;
  29.             prev = current;
  30.             current = foward;
  31.         }
  32.  
  33.          while(prev!=null){
  34.              System.out.println(prev.data);
  35.             prev =prev.next;
  36.         }
  37.      
  38.  
  39.     }
  40.  
  41.     private static final Scanner scanner = new Scanner(System.in);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement