Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

method "count" optimized to O(1) #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 27 additions & 30 deletions LinkedList/LinkedList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,38 @@
* Created by gazollajunior on 07/04/16.
*/

class Node<T>(value: T){
var value:T = value
class Node<T>(value: T) {
var value: T = value
var next: Node<T>? = null
var previous:Node<T>? = null
var previous: Node<T>? = null
}

class LinkedList<T> {

private var head:Node<T>? = null
private var head: Node<T>? = null

var isEmpty:Boolean = head == null
private var _count = 0

fun first():Node<T>? = head
var isEmpty: Boolean = head == null

fun last(): Node<T>? {
var node = head
if (node != null){
while (node?.next != null) {
node = node?.next
}
return node
} else {
return null
}
}
fun first(): Node<T>? = head

fun count():Int {
fun last(): Node<T>? {
var node = head
if (node != null){
var counter = 1
while (node?.next != null){
if (node != null) {
while (node?.next != null) {
node = node?.next
counter += 1
}
return counter
return node
} else {
return 0
return null
}
}

fun nodeAtIndex(index: Int) : Node<T>? {
val count
get() = _count

fun nodeAtIndex(index: Int): Node<T>? {
if (index >= 0) {
var node = head
var i = index
Expand All @@ -65,13 +56,15 @@ class LinkedList<T> {
} else {
head = newNode
}
_count++
}

fun removeAll() {
head = null
_count = 0
}

fun removeNode(node: Node<T>):T {
fun removeNode(node: Node<T>): T {
val prev = node.previous
val next = node.next

Expand All @@ -84,22 +77,24 @@ class LinkedList<T> {

node.previous = null
node.next = null

_count--
return node.value
}

fun removeLast() : T? {
fun removeLast(): T? {
val last = this.last()
if (last != null) {
_count--
return removeNode(last)
} else {
return null
}
}

fun removeAtIndex(index: Int):T? {
fun removeAtIndex(index: Int): T? {
val node = nodeAtIndex(index)
if (node != null) {
_count--
return removeNode(node)
} else {
return null
Expand All @@ -112,7 +107,9 @@ class LinkedList<T> {
while (node != null) {
s += "${node.value}"
node = node.next
if (node != null) { s += ", " }
if (node != null) {
s += ", "
}
}
return s + "]"
}
Expand Down