fork(1) download
  1. public class LinkedList<E> {
  2. private Node<E> head = null;
  3.  
  4. private class Node<E> {
  5. E value;
  6. Node<E> next;
  7.  
  8. // Node constructor links the node as a new head
  9. Node(E value) {
  10. this.value = value;
  11. this.next = head;
  12. head = this;
  13. }
  14. }
  15.  
  16. public void add(E e) {
  17. new Node<E>(e);
  18. // Link node as new head
  19. }
  20.  
  21. public void dump() {
  22. for (Node<E> n = head; n != null; n = n.next)
  23. System.out.print(n.value + " ");
  24. }
  25.  
  26. public static void main(String[] args) {
  27. LinkedList<String> list = new LinkedList<>();
  28. list.add("world");
  29. list.add("Hello");
  30. list.dump();
  31. }
  32. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:1: error: class LinkedList is public, should be declared in a file named LinkedList.java
public class LinkedList<E> {
       ^
Main.java:11: error: incompatible types
            this.next = head;
                        ^
  required: LinkedList<E#1>.Node<E#2>
  found:    LinkedList<E#1>.Node<E#1>
  where E#1,E#2 are type-variables:
    E#1 extends Object declared in class LinkedList
    E#2 extends Object declared in class LinkedList.Node
Main.java:12: error: incompatible types
            head = this;
                   ^
  required: LinkedList<E#2>.Node<E#2>
  found:    LinkedList<E#2>.Node<E#1>
  where E#1,E#2 are type-variables:
    E#1 extends Object declared in class LinkedList.Node
    E#2 extends Object declared in class LinkedList
3 errors
stdout
Standard output is empty