Question
// I JUST NEED THE LINKED LIST TO BE SORTED IN SWIFT // SO IMPLEMENT A FUNCTION THAT WILL SORT THE LIST // FOR EXAMPLE
// I JUST NEED THE LINKED LIST TO BE SORTED IN SWIFT
// SO IMPLEMENT A FUNCTION THAT WILL SORT THE LIST
// FOR EXAMPLE func sortList()
import UIKit
class Node
{
var value : Int
var next : Node?
init(value : Int, next : Node?)
{
self.value = value
self.next = next
}
}
class LinkedList
{
var head : Node?
func insertNode(value : Int)
{
if head == nil
{
head = Node(value: value, next: nil)
return
}
var currentNode = head
while currentNode?.next != nil {
currentNode = currentNode?.next
}
currentNode?.next = Node(value: value, next: nil)
}
func displayList()
{
var currentNode = head
while currentNode != nil {
print(currentNode?.value ?? "")
currentNode = currentNode?.next
}
}
func deleteNode(value : Int)
{
if head?.value == value
{
head = head?.next
return
}
var currentNode = head
var previousNode : Node?
while currentNode != nil && currentNode?.value != value{
previousNode = currentNode
currentNode = currentNode?.next
}
previousNode?.next = currentNode?.next
}
func displayEveryOtherNode()
{
var currentNode = head
while currentNode != nil {
print(currentNode?.value ?? "")
currentNode = currentNode?.next?.next
}
}
}
let linkedList = LinkedList()
var index = 0
while index < 9 {
let randomInt = Int.random(in: 1...30)
linkedList.insertNode(value: randomInt)
index = index + 1
}
linkedList.displayList()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started