fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10.  
  11. public static class Node {
  12. int value;
  13. Node next;
  14. }
  15.  
  16. static class LinkedList {
  17. Node first;
  18. }
  19.  
  20.  
  21. public static void main(String[] args) {
  22. Node n1 = new Node();
  23. n1.value = 3;
  24. Node n2 = new Node();
  25. n2.value = 2;
  26. n2.next = n1;
  27. Node n3 = new Node();
  28. n3.value = 1;
  29. n3.next = n2;
  30.  
  31. LinkedList list = new LinkedList();
  32. list.first = n3;
  33.  
  34. Node tNode = list.first;
  35. while (tNode != null) {
  36. System.out.println(tNode.value + " ");
  37. tNode = tNode.next;
  38. }
  39.  
  40. tNode = list.first;
  41. Node oldNext = null;
  42. while (tNode != null) {
  43. Node next = tNode.next;
  44. tNode.next = oldNext;
  45. oldNext = tNode;
  46. if(next == null) {
  47. list.first = tNode;
  48. }
  49. tNode = next;
  50. }
  51.  
  52. tNode = list.first;
  53. while (tNode != null) {
  54. System.out.println(tNode.value + " ");
  55. tNode = tNode.next;
  56. }
  57. }
  58. }
Success #stdin #stdout 0.04s 320576KB
stdin
Standard input is empty
stdout
1 
2 
3 
3 
2 
1