Kripthonite
Kripthonite

Reputation: 71

Implementation of a kotlin function

Trying to implement this function fun <E> intersection(list1: Node<E>, list2: Node<E>, cmp: Comparator<E>): Node<E> {}

given the doubly linked, circular, sentinel lists referenced by list1 and list2, and sorted in ascending order according to the cmp comparator, returns a list with the elements that are simultaneously present in list1 and list2, removing them in both lists. the list returned should be doubly linked, non-circular and without sentinel, sorted in ascending order. must still reuse the nodes of one of the lists (list1 or list2) and cannot contain repeated elements..

Node implementation :

class Node<E> {
    var previous: Node<E>? = null
    var next: Node<E>? = null
    var value: E? = null
}

I ve been on this problem for like 2 days. i really cant grasp what ive been doing wrong. I would be really grateful if someone could help me

this is what i have so far , i can return the right values of the new List , but cant delete the nodes in list1 and list2 without ruining the iteration of the loops

fun <E> intersection(list1: Node<E>, list2: Node<E>, cmp: Comparator<E>): Node<E>? {
    //var arrayList =ArrayList<Node<E>>()
    var arr = ArrayList<E?>()
    var list: Node<E>? = null
    var temp = list1
    var temp2 = list2
    var count = 0
    var head : Node<E>? = null
    while (temp.next?.value != null){
        temp = temp.next!!

        while(temp2.next?.value !=null){
            temp2 = temp2.next!!
            if(cmp.compare(temp.value,temp2.value)==0 && !arr.contains(temp.value)){


                var novo = temp


                if (list != null){
                    novo.previous = list
                    list.next = novo
                }
                list = novo

                count ++
                if(count==1){
                    list.previous = null
                    head = list
                }

                arr.add(temp.value)
                break;
            }

        }

Upvotes: 1

Views: 113

Answers (1)

Kripthonite
Kripthonite

Reputation: 71

fun <E> intersection(list1: Node<E>, list2: Node<E>, cmp: Comparator<E>): Node<E>? {
//var arrayList =ArrayList<Node<E>>()

var list: Node<E>? = null
var temp = list1
var temp2 = list2
var count = 0
var head : Node<E>? = null
while (temp.next?.value != null){
    temp = temp.next!!

    while(temp2.next?.value !=null){
        temp2 = temp2.next!!
        if(cmp.compare(temp.value,temp2.value)==0 ){
            var novo = deleteNode(temp)
            if (list != null){
                novo.previous = list
                list.next = novo
            }
            list = novo
            count ++
            if(count==1){
                list.previous = null
                head = list
            }

            deleteNode(temp2)
            break;
        }

    }
    temp2 = list2

}


return head}




fun <E> deleteNode(node : Node<E>): Node<E>{

var prev = node.previous
var next = node.next
//var temp = node

while(next!=null && next!!.value == node.value ){ // eliminado duplicados
    next = next.next
    //temp = temp.next!!
}


if (prev != null) {
    prev.next = next
}
if (next != null) {
    next.previous = prev
}

return node}

Here is the resolution to my own problem. This was a headache. Hope this helps someone

Upvotes: 1

Related Questions