Codecademy Logo

Linked Lists

Linked List Overview

A LinkedList (singly-linked list) class in Swift has the following attributes:

  • a head Node variable stored property
  • a tail Node variable stored property
  • .append(_:) method to add new nodes to the tail
  • .getNode(at:) method to retrieve a node by index
  • .removeNode(at:) to remove a node by index
  • an extension that uses the CustomStringConvertible protocol to output a human-readable ordered list of node data values
class LinkedList {
var head: Node?
var tail: Node?
func append(_ data: String) {
let newNode = Node(data: data)
if let lastNode = tail {
lastNode.next = newNode
} else {
head = newNode
}
tail = newNode
}
func getNode(at index: Int) -> Node? {
guard index >= 0 else { return nil }
var current = head
for _ in 0..<index {
guard let next = current?.next else { return nil }
current = next
}
return current
}
func removeNode(at index: Int) -> Node? {
var removedNode: Node?
guard index >= 0 else { return nil }
if index == 0 {
removedNode = head
head = head?.next
if head == nil {
tail = nil
}
return removedNode
}
guard let previous = getNode(at: index - 1) else { return nil }
removedNode = previous.next
if removedNode?.next == nil {
tail = previous
}
previous.next = removedNode?.next
return removedNode
}
}
extension LinkedList: CustomStringConvertible {
var description: String {
return head?.description ?? "nil"
}
}

Removing a node from the middle of a linked list

When removing a node from the middle of a linked list, it is necessary to adjust the link on the previous node so that it points to the following node. In the given illustration, the node a1 must point to the node a3 if the node a2 is removed from the linked list.

removing a node

Linked List data structure

A linked list is a linear data structure where elements are not stored at contiguous location. Instead the elements are linked using pointers.

In a linked list data is stored in nodes and each node is linked to the next and, optionally, to the previous. Each node in a list consists of the following parts:

  1. data
  2. A pointer (Or reference) to the next node
  3. Optionally, a pointer to the previous node
A linked list with each node holding its data and pointing to the next node.

Adding a new head node in a linked list

When adding a new node to the start of a linked list, it is necessary to maintain the list by giving the new head node a link to the current head node. For instance, to add a new node a0 to the begining of the linked list, a0 should point to a1.

adding new node

The Head Node in Linked Lists

The first node in a linked list is called the head node. If the linked list is empty, then the value of the head node is NULL.

A linked list structure with Head and Tail pointers.

Implementing a linked list

A linked list exposes the ability to traverse the list from one node to another node. The starting node is considered the head node from where the list can be traversed.

a linked list

Learn More on Codecademy