Linked List – Part 1

This is a two-part tutorial.

A linked list is a collection of values arranged in a linear one directional sequence where each node is linked to its successor. It has several advantages over a contiguous storage solution such as the Array.

  • Constant time insertion and removal from the front of the list
  • Reliable performance


Nodes have to responsibilities

  • Hold a value
  • Hold a link or reference to the next node. Unless it is at the end of the list where a nil value is in place.

Let’s create a Node class.  Open Xcode and start a  new project. Name it what you want but  I’m calling mine “Linked List”.

Once you’ve created the playground create a new source file and call it Node.swift. Then copy and add the following lines of code

If you’re unable to add a source folder, within the playground view, hold cmd and 0. You should then be able to see the project navigator on the right-hand side. Right-click the sources file and tap new file.

Again add a new file but this time call it “Helper” and paste the following code

This will simply print out the function into the console.

Finally, in the main playground write the following code.

Congratulations, you’ve just created three nodes and connected them. To confirm you should see the following output in your console

But, theoretically speaking this isn’t too practical and building lists are going to take you an age. So now let’s create a class that manages the Node objects and solve this issue.


Add the following class in the sources folder

So just like a snake having a head and tail, the head refers to the first Node and the tail to the last.


In part two of this tutorial, we will create an interface to manage the Node objects. We will add, delete then finally make the collection as close to swift as possible.