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. class linkedlist{
  8. public int data;
  9. public linkedlist next;
  10. public linkedlist(int data){
  11. this.data=data;
  12. }
  13. public linkedlist(int data,linkedlist next){
  14. this.data=data;
  15. this.next=next;
  16. }
  17. }
  18. /* Name of the class has to be "Main" only if the class is public. */
  19.  
  20. class Ideone
  21. {
  22. public static linkedlist head;
  23. public static linkedlist insert(int data){
  24.  
  25. if(head==null){
  26. head=new linkedlist(data);
  27. }
  28. else{
  29. linkedlist temp=head;
  30. while(temp.next!=null){
  31. temp=temp.next;
  32. }
  33. temp.next=new linkedlist(data);
  34. }
  35. return head;
  36. }
  37. public static void print(linkedlist head){
  38. while(head!=null){
  39. System.out.println(head.data);
  40. head=head.next;
  41. }
  42. }
  43. public static void main (String[] args) throws java.lang.Exception
  44. {
  45. head=insert(1);
  46. head=insert(2);
  47. head=insert(3);
  48. head=insert(4);
  49. head=insert(5);
  50. print(head);
  51.  
  52. }
  53. }
Success #stdin #stdout 0.08s 50952KB
stdin
Standard input is empty
stdout
1
2
3
4
5