Warning: Use of undefined constant fixed - assumed 'fixed' (this will throw an Error in a future version of PHP) in /home/webtro13/domains/farenda.com/public_html/wp-content/themes/moments/header.php on line 65
class="header_container_flat">

Linked List is a simple data structure. Implementing it from scratch helps to understand its properties. We’ll implement Linked List add, size, and print its contents.

Linked List construction

To construct Linked List we’ll need two things. One is an internal representation of Linked List Node, the other is the reference to the first element of the list – the head. This can be represented like here:

Print content of Linked List

To help ourselves to verify results of our work we’ll start with implementation of a helper method that prints the contents of a Linked List. As many algorithms on Linked Lists this one works in linear time – O(N), because it has to go through the whole list:

Again, the algorithm has linear time complexity O(N), because it has to traverse whole list to add element at the end.

Count number of elements in Linked List

Another useful method to have is list.size(), which will return the number of elements in Linked List. It works almost exactly the same as addition, therefore it’s running time is proportional to the size of the list, so its O(N):