<p>What's the best way to make a linked list in Java?</p>

[{'answer_id': 10050, 'author': 'dlinsin', 'author_id': 198, 'author_profile': 'https://Stackoverflow.com/users/198', 'pm_score': 6, 'selected': False, 'text': '<p>Java has a <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/LinkedList.html" rel="noreferrer">LinkedList</a> implementation, that you might wanna check out. You can download the JDK and it\'s sources at <a href="http://java.sun.com/javase/downloads/index.jsp" rel="noreferrer">java.sun.com</a>.</p>\n'}, {'answer_id': 12432, 'author': 'Juha Syrjälä', 'author_id': 1431, 'author_profile': 'https://Stackoverflow.com/users/1431', 'pm_score': 5, 'selected': False, 'text': '<p>Use <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/LinkedList.html" rel="noreferrer">java.util.LinkedList</a>. Like this:</p>\n\n<pre><code>list = new java.util.LinkedList()\n</code></pre>\n'}, {'answer_id': 25311, 'author': 'Aaron', 'author_id': 2628, 'author_profile': 'https://Stackoverflow.com/users/2628', 'pm_score': 9, 'selected': True, 'text': '<p>The obvious solution to developers familiar to Java is to use the <strong>LinkedList</strong> class already provided in <strong>java.util</strong>. Say, however, you wanted to make your own implementation for some reason. Here is a quick example of a linked list that inserts a new link at the beginning of the list, deletes from the beginning of the list and loops through the list to print the links contained in it. <strong>Enhancements</strong> to this implementation include making it a <strong>double-linked list</strong>, adding methods to <strong>insert</strong> and <strong>delete</strong> from the middle or end, and by adding <strong>get</strong> and <strong>sort</strong> methods as well. </p>\n\n<p><strong>Note</strong>: In the example, the Link object doesn\'t actually contain another Link object -  <em>nextLink</em> is actually only a reference to another link. </p>\n\n<pre><code>class Link {\n    public int data1;\n    public double data2;\n    public Link nextLink;\n\n    //Link constructor\n    public Link(int d1, double d2) {\n        data1 = d1;\n        data2 = d2;\n    }\n\n    //Print Link data\n    public void printLink() {\n        System.out.print("{" + data1 + ", " + data2 + "} ");\n    }\n}\n\nclass LinkList {\n    private Link first;\n\n    //LinkList constructor\n    public LinkList() {\n        first = null;\n    }\n\n    //Returns true if list is empty\n    public boolean isEmpty() {\n        return first == null;\n    }\n\n    //Inserts a new Link at the first of the list\n    public void insert(int d1, double d2) {\n        Link link = new Link(d1, d2);\n        link.nextLink = first;\n        first = link;\n    }\n\n    //Deletes the link at the first of the list\n    public Link delete() {\n        Link temp = first;\n        if(first == null){\n         return null;\n         //throw new NoSuchElementException(); // this is the better way. \n        }\n        first = first.nextLink;\n        return temp;\n    }\n\n    //Prints list data\n    public void printList() {\n        Link currentLink = first;\n        System.out.print("List: ");\n        while(currentLink != null) {\n            currentLink.printLink();\n            currentLink = currentLink.nextLink;\n        }\n        System.out.println("");\n    }\n}  \n\nclass LinkListTest {\n    public static void main(String[] args) {\n        LinkList list = new LinkList();\n\n        list.insert(1, 1.01);\n        list.insert(2, 2.02);\n        list.insert(3, 3.03);\n        list.insert(4, 4.04);\n        list.insert(5, 5.05);\n\n        list.printList();\n\n        while(!list.isEmpty()) {\n            Link deletedLink = list.delete();\n            System.out.print("deleted: ");\n            deletedLink.printLink();\n            System.out.println("");\n        }\n        list.printList();\n    }\n}\n</code></pre>\n'}, {'answer_id': 690660, 'author': 'Jakub Arnold', 'author_id': 72583, 'author_profile': 'https://Stackoverflow.com/users/72583', 'pm_score': 3, 'selected': False, 'text': "<p>Its much better to use java.util.LinkedList, because it's probably much more optimized, than the one that you will write.</p>\n"}, {'answer_id': 1963840, 'author': 'arnab sarkar', 'author_id': 238899, 'author_profile': 'https://Stackoverflow.com/users/238899', 'pm_score': 4, 'selected': False, 'text': '<p>The above linked list display in opposite direction. I think the correct implementation of insert method should be</p>\n\n<pre><code>public void insert(int d1, double d2) { \n    Link link = new Link(d1, d2); \n\n    if(first==null){\n        link.nextLink = null;\n        first = link; \n        last=link;\n    }\n    else{\n        last.nextLink=link;\n        link.nextLink=null;\n        last=link;\n    }\n} \n</code></pre>\n'}, {'answer_id': 6674458, 'author': 'Anitha A', 'author_id': 842022, 'author_profile': 'https://Stackoverflow.com/users/842022', 'pm_score': 3, 'selected': False, 'text': '<pre><code>//slightly improved code without using collection framework\n\npackage com.test;\n\npublic class TestClass {\n\n    private static Link last;\n    private static Link first;\n\n    public static void main(String[] args) {\n\n        //Inserting\n        for(int i=0;i&lt;5;i++){\n            Link.insert(i+5);\n        }\n        Link.printList();\n\n        //Deleting\n        Link.deletefromFirst();\n        Link.printList();\n    }\n\n\n    protected  static class Link {\n        private int data;\n        private Link nextlink;\n\n        public Link(int d1) {\n            this.data = d1;\n        }\n\n        public static void insert(int d1) {\n            Link a = new Link(d1);\n            a.nextlink = null;\n            if (first != null) {\n                last.nextlink = a;\n                last = a;\n            } else {\n                first = a;\n                last = a;\n            }\n            System.out.println("Inserted -:"+d1);\n        }\n\n        public static void deletefromFirst() {\n            if(null!=first)\n            {\n                System.out.println("Deleting -:"+first.data);\n                first = first.nextlink;\n            }\n            else{\n                System.out.println("No elements in Linked List");\n            }\n        }\n\n        public static void printList() {\n            System.out.println("Elements in the list are");\n            System.out.println("-------------------------");\n            Link temp = first;\n            while (temp != null) {\n                System.out.println(temp.data);\n                temp = temp.nextlink;\n            }\n        }\n    }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/10042', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/571/']