Skip to content

alexpaul/LinkedList

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 

Repository files navigation

Node and Linked List

Node

A node is an abstract data structure that consists of a value and next pointer property.

Node class

class Node {
  var value: Int
  var next: Node?
  init(_ value: Int) {
    self.value = value
  }
}

Write a function to print all the values in a linked node (list)

func printNodeValues(node: Node?) {
  var node = node
  while let currentNode = node {
    print(currentNode.value) 
    node = currentNode.next // moving to the next node using the `next` pointer
  }
}

printNodeValues(node: node1) // 1 2 3

Write a function to reverse values in a linked node (list)

func reverseList(_ node: Node?) -> Node? {
  var node = node
  var previousNode: Node?
  var nextNode: Node?
  while let currentNode = node {
    nextNode = currentNode.next
    currentNode.next = previousNode
    previousNode = currentNode
    node = nextNode
  }
  return previousNode
}

printNodeValues(node: reverseList(node1)) // 3 2 1

Linked List

A linked list is a series of connected nodes.

Types of a linked list:

  • singly linked list
  • doubly linked list

A linked list has a head and tail property.

Define a Linked List

struct LinkedList {
  private var head: Node?
  private var tail: Node?
  
  var isEmpty: Bool {
    return head == nil
  }
}

Write a function to add a value to a linked list

extension LinkedList {
  mutating func append(_ value: Int) {
    // 1
    // create a new node with the value
    let newNode = Node(value)
    // 2
    // if the list is empty create a new node and add to the head and tail
    if isEmpty {
      head = newNode
      tail = newNode
      return
    }
    // if there are more values in the list, we need to have the (last last node point to the new node)
    guard let lastNode = tail else {
      return
    }
    lastNode.next = newNode
    tail = newNode
  }
}

var list = LinkedList() 
list.append(5)
list.append(12)
list.append(0)
list.append(34)

Write a function to print the values of a linked list

extension LinkedList {
  func printList() {
    var head = self.head
    while let currentNode = head {
      print(currentNode.value)
      head = currentNode.next
    }
  }
}
var list = LinkedList() 
list.append(5)
list.append(12)
list.append(0)
list.append(34)

list.printList()

Challenge - Write a function to remove the last element from a linked list

Solution

Sorry was hoping you would give it a shot 🥳

Challenges

Challenge 1

LeetCode - Middle of a Linked List

Challenge 2

LeetCode - Remove Linked List Elements

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published