Fixing remove runtime
parent
b5a109d8e8
commit
4e1ecbc83c
|
@ -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>
|
||||
|
|
Loading…
Reference in New Issue