An iterable implementation of LinkedList I am curious as to what improvements to my code can be suggested.

package concepts;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class LinkedList&lt;M&gt; implements Iterable&lt;LinkedListNode&lt;M&gt;&gt; {

    public static class LinkedListIterator&lt;O&gt; implements Iterator&lt;LinkedListNode&lt;O&gt;&gt; {
        private LinkedListNode&lt;O&gt; curNode;

        public LinkedListIterator(LinkedList&lt;O&gt; linkedList) {
            curNode = linkedList.head;
        }

        @Override
        public boolean hasNext() {
            if (curNode != null) {
                return true;
            } else {
                return false;
            }
        }

        @Override
        public LinkedListNode&lt;O&gt; next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            } else {
                LinkedListNode&lt;O&gt; temp = curNode;
                curNode = curNode.next;
                return temp;
            }

        }

    }

    public LinkedListNode&lt;M&gt; head;

    public boolean add(M newData) {
        LinkedListNode&lt;M&gt; newNode = new LinkedListNode&lt;M&gt;();
        newNode.data = newData;
        if (head == null) {
            head = newNode;
        } else {
            LinkedListIterator&lt;M&gt; it = new LinkedListIterator&lt;M&gt;(this);
            LinkedListNode&lt;M&gt; lastNode = null;
            while (it.hasNext()) {
                lastNode = it.next();
            }
            if (lastNode != null) {
                lastNode.next = newNode;
            } else {
                throw new IllegalStateException();
            }
        }
        return true;
    }

    @Override
    public Iterator&lt;LinkedListNode&lt;M&gt;&gt; iterator() {
        return new LinkedListIterator&lt;M&gt;(this);
    }

}


Here is the LinkedListNode implementation:

package concepts;

public class LinkedListNode&lt;N&gt; {
    public N data;
    public LinkedListNode&lt;N&gt; next;
}
Antipattern

It's not a good idea to expose the internal list nodes, and this is why:

public static void main(String[] args) {
    LinkedList&lt;Integer&gt; list = new LinkedList&lt;&gt;();
    list.add(123);
    // Make a loop:
    list.head.next = list.head;
    int i = 0;

    for (LinkedListNode&lt;Integer&gt; node : list) {
        ++i;

        if (i == 100) {
            break;
        }
    }

    System.out.println("i = " + i);
}


As the above snippet demonstrates, a user may restructure your linked list into a cycle in which iteration will never terminate. So the moral here is that:

hide implementation from the user!

First of all, make head a private field. Then, reimplement the iterator that iterates over values and not the internal nodes holding those values.

Additional field tail would not hurt, while keeping your add method a constant time operation.

Summa summarum

All in all, I had this in mind:

package concepts;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class LinkedListCR&lt;E&gt; implements Iterable&lt;E&gt; {

    private static final class LinkedListNode&lt;E&gt; {
        private E element;
        private LinkedListNode&lt;E&gt; next;

        LinkedListNode(E element) {
            this.element = element;
        }
    }

    private class LinkedListIterator implements Iterator&lt;E&gt; {

        private LinkedListNode&lt;E&gt; nextNodeToReturn = head;

        @Override
        public boolean hasNext() {
            return nextNodeToReturn != null;
        }

        @Override
        public E next() {
            if (!hasNext()) {
                throw new NoSuchElementException("Iterator exceeded.");
            }

            E ret = nextNodeToReturn.element;
            nextNodeToReturn = nextNodeToReturn.next;
            return ret;
        }

    }

    private LinkedListNode&lt;E&gt; head;
    private LinkedListNode&lt;E&gt; tail;

    public void add(E element) {
        LinkedListNode&lt;E&gt; newnode = new LinkedListNode&lt;&gt;(element);

        if (head == null) {
            head = newnode;
            tail = newnode;
        } else {
            tail.next = newnode;
            tail = newnode;
        }
    }

    @Override
    public Iterator&lt;E&gt; iterator() {
        return new LinkedListIterator();
    }

    public static void main(String[] args) {
        LinkedListCR&lt;String&gt; list = new LinkedListCR&lt;&gt;();
        list.add("hello");
        list.add("world");
        list.add("y'all");

        for (String s : list) {
            System.out.print(s + " ");
        }

        System.out.println();
    }
}


Hope that helps.