Data Structure: Linked List in Swift 4

Linked List


Creating Node

We have created Node class with value and next variables. It has a printNode method which prints all the nodes linked with it. Let’s create the Linked List class.

Creating Linked List Class

We have created Linked list class which has the parent node and the last node.

CreateLinkedListWithElements method creates the linked list with the given array.

Initializing Linked List

We have created linked list object with the integer array and then we print the linked list node by node

Appending Items in the linked list

Now use this method to append the elements to the linked list we create just before.

So appending elements to the linked list is quite easy.

Inserting a value in the Linked list

Now let’s see how we can use it

As you can see, we inserted value 64 at index 8 in the list we created before. It inserts the value and prints the whole list node by node

Removing Node from Linked List

Now let’s see how we can use it.

As you can see we have removed the node at position 2 which is 2 in this case and it prints all the node one by one after removal.

Get the last value

Let’s use it to get the value

Get Value at Index

Write this method in your linked list class

Let’s use this method to get the value at position 3 from the linked list we created.

Get the length of the linked list

Let’s use it.




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store