Mastering Linked Lists in Swift: A Comprehensive Guide

Rohan Patel
3 min readJan 7, 2023

--

Linked lists are a linear data structure that consists of a sequence of nodes, each pointing to the next node in the list. They are useful for cases where you need to insert or delete elements from the list frequently, as they do not require the relocation of elements in memory like arrays do. In this article, we will go over how to implement a simple linked list algorithm in Swift.

First, let’s define a Node class that will represent each element in the list:

class Node {
var value: Int
var next: Node?

init(value: Int) {
self.value = value
}
}

The Node class has two properties: value, which stores the value of the element, and next, which is an optional reference to the next element in the list.

Next, let’s define a LinkedList class that will hold the list of nodes:

class LinkedList {
var head: Node?

func append(value: Int) {
let newNode = Node(value: value)

if head == nil {
head = newNode
return
}

var current = head
while current?.next != nil {
current = current?.next
}
current?.next = newNode
}
}

The LinkedList class has a single property: head, which is an optional reference to the first element in the list. The append method is used to add a new element to the end of the list.

To use our linked list, we can create an instance of the LinkedList class and use the append method to add elements:

let list = LinkedList()
list.append(value: 1)
list.append(value: 2)
list.append(value: 3)

This will create a linked list with the values 1, 2, and 3.

We can also implement other operations for our linked list, such as inserting an element at a specific index, deleting an element, and finding the value at a specific index.

Here is the complete code for our LinkedList class:

class LinkedList {
var head: Node?

func append(value: Int) {
let newNode = Node(value: value)

if head == nil {
head = newNode
return
}

var current = head
while current?.next != nil {
current = current?.next
}
current?.next = newNode
}

func insert(value: Int, at index: Int) {
let newNode = Node(value: value)

if index == 0 {
newNode.next = head
head = newNode
return
}

var current = head
var currentIndex = 0
while current?.next != nil && currentIndex < index - 1 {
current = current?.next
currentIndex += 1
}

newNode.next = current?.next
current?.next = newNode
}

func delete(at index: Int) {
if index == 0 {
head = head?.next
return
}

var current = head
var currentIndex = 0
while current?.next != nil && currentIndex < index - 1 {
current = current?.next
currentIndex += 1
}

current?.next = current?.next?.next
}

func value(at index: Int) -> Int? {
var current = head
var currentIndex = 0
while current != nil && currentIndex < index {
current = current?.next
currentIndex += 1
}

return current?.value
}
}

To insert an element at a specific index, we simply iterate through the list until we reach the desired index, and then update the `next` pointers accordingly. To delete an element, we do the same thing but skip over the element by updating the `next` pointer of the previous element to point to the element after the one we want to delete. To find the value at a specific index, we iterate through the list until we reach the desired index and return the value of the current node. I hope this helps you understand how to implement a simple linked list algorithm in Swift. Linked lists are a useful data structure to have in your toolkit, and they can be used to solve a variety of problems.

--

--