Fixing remove runtime

pull/2/head
Oliver Kennedy 2022-09-21 14:03:52 -04:00
parent b5a109d8e8
commit 4e1ecbc83c
Signed by: okennedy
GPG Key ID: 3E5F9B3ABD3FDB60
1 changed files with 12 additions and 13 deletions

View File

@ -75,8 +75,8 @@ textbook: "Ch. 7"
def length: Int =
{
var i = 0
var curr = head
while(curr.isDefined){ i += 1; curr = curr.get.next }
var current = head
while(current.isDefined){ i += 1; curr = curr.get.next }
return i
}
</code></pre>
@ -118,14 +118,13 @@ textbook: "Ch. 7"
<pre><code class="scala">
def apply(idx: Int): T =
{
var i = 0
var curr = head
while(i < idx){
if(curr.isEmpty) { throw IndexOutOfBoundsException(idx) }
i += 1; curr = curr.get.next
var current = head
for(i <- 0 until idx){
if(current.isEmpty) { throw IndexOutOfBoundsException(idx) }
current = current.get.next
}
if(curr.isEmpty) { throw IndexOutOfBoundsException(idx) }
return curr
if(current.isEmpty) { throw IndexOutOfBoundsException(idx) }
return current
}
</code></pre>
@ -151,10 +150,10 @@ textbook: "Ch. 7"
if(idx == 0){
head = Some( new SinglyLinkedListNode(value, head) )
} else {
var i = 1; var curr = head
while(i < idx){
var current = head
for(i <- 0 until idx){
if(curr.isEmpty) { throw IndexOutOfBoundsException(idx) }
i += 1; curr = curr.get.next
curr = curr.get.next
}
curr.next = Some( new SinglyLinkedListNode(value, curr.next) )
}
@ -176,7 +175,7 @@ textbook: "Ch. 7"
def sum(list: List[Int]): Unit =
{
val total: Int = 0
for(i <- 0 until list.size){ total += list(i) }
for(i <- 0 until list.length){ total += list(i) }
return total
}
</code></pre>