Microsoft - Find Biggest Node

 

 

 

public Node findBiggest (Node n1, Node n2){

    Node c1 = n1;
    Node c2 = n2;
    boolean isPositive = false;
    if(n1.value == '-'){
        if(n2.value != '-'){
            return n2;
        }
        c1=n1.next;
        if(n2.value == '-'){
            isPositive = false;
        c2=n2.next;
}
}
else{
if(n2.value == '-'){
    return n1;
}
isPositive = true;
if(n2.value  == '+'){
    c2 = n2.next;
}
if(n1.value == '+'){
    c1 = n1.next;
}
}
    while(c1 != null && c2 != null){
        int d1 = c1.value - '0';
        int d2 = c2.value - '0';

        if(d1 > d2){
            if(isPositive){
    return n1;
}
else{
    return n2;
}
}
else if(d1 < d2){
    if(isPositive){
        return n2;
}
else{ 
return n1;
}
}
else{
    c1 = c1.next;
    c2 = c2.next;
}
}
if(c2 != null && isPositive){
    return n2;
}
if(c2 != null && !isPositive){
    return n1;
}
if(c1 != null && isPositive){
    return n1;
}
if(c1 != null && !isPositive){
    return n2;
}
    return null;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325011156&siteId=291194637