It can be useful to have "housekeeping" information in the the list class:
One can use an entire dummy node as the head, rather than simply a pointer:
This can eliminate some special cases in the insert/delete code.
The list type we have defined uses "internal" data for its nodes. Often one defines "external" data instead:
One other important variation on linked lists is to have list nodes point both to the next and the previous element. Among other things, this makes it easy to traverse a list backwards:
struct ListNode { int value; ListNode * next, *prev; }; class LinkedList { // some stuff, then: private: ListNode * head, * last; }; // Reverse traversal void List::printReverseOrder() { ListNode * current = last; while (current != NULL) { cout << current->value << endl; current = current->prev; } }