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. /* package codechef; // don't place package name! */
  8.  
  9. import java.util.*;
  10. import java.lang.*;
  11. import java.io.*;
  12.  
  13. /* Name of the class has to be "Main" only if the class is public. */
  14. {
  15. Node head;
  16.  
  17. static class Node{
  18. int data;
  19. Node next;
  20.  
  21. Node(int ele)
  22. {
  23. data = ele;
  24. next=null;
  25. }
  26. }
  27.  
  28.  
  29. private void createList(int ele)
  30. {
  31. System.out.println("Inside createList");
  32. Node n = new Node(ele);
  33. if(head == null)
  34. {
  35. head = n;
  36. }
  37. else{
  38. Node last = head;
  39. while(last.next != null)
  40. {
  41. last = last.next;
  42. }
  43. last.next = n;
  44. }
  45. }
  46.  
  47. private void traverseList()
  48. {
  49. Node current = head;
  50.  
  51. while(current != null)
  52. {
  53. System.out.println(current.data);
  54. current= current.next;
  55. }
  56. }
  57.  
  58. public static void main (String[] args) throws java.lang.Exception
  59. {
  60. LinkedList l1 = new LinkedList();
  61. l1.createList(2);
  62. l1.createList(4);
  63. l1.createList(5);
  64. l1.createList(6);
  65. l1.createList(27);
  66.  
  67. l1.traverseList();
  68.  
  69. }
  70. }
  71.  
Success #stdin #stdout 0.04s 2184192KB
stdin
Standard input is empty
stdout
Inside createList
Inside createList
Inside createList
Inside createList
Inside createList
2
4
5
6
27