找到链表的第二个最大键?

时间:2017-01-08 03:02:09

标签: java list

请帮我修改我的代码。有些案例有效但我认为像[9,1,2]这样的案例不起作用,因为在这种情况下第一个数字是最大的。我不知道如何在我的代码中修复它。

public Key secondMaxKey () {
    Node max = first;
    Node max_2nd = first;

    if (size() < 2) {
        return null;
    }

    for (Node x = first; x != null;x = x.next) {

        if (x.key.compareTo(max.key) == 1) {
            max_2nd = max;
            max = x;
        } else if (x.key.compareTo(max_2nd.key) == 1) {
            max_2nd = x;
        }
    }
    return max_2nd.key;
}

5 个答案:

答案 0 :(得分:1)

在我看来,您首先检查大小然后继续算法。 这里稍微改变一下,因为我们使用前两个节点来设置max和max_2(取决于它们的值)。然后我们继续你的行动。 看看这个。希望它有所帮助

public Key secondMaxKey () {
            if(size() < 2){
                return null;
            }
            Node max = null;
            Node max_2 = null;

            Node second = first.next;

            if(first.key.compareTo(second.key) > 0){
                max = first;
                max_2 = second;
            }   else{
                max = second;
                max_2 = first;
            }

            for (Node x=second.next; x != null;x=x.next)
            {
                if (x.key.compareTo(max.key) > 0)
                {
                    max_2=max;
                    max=x;
                }
                else if ((x.key.compareTo(max_2.key) > 0)
                        && (x.key.compareTo(max.key) < 0))
                {
                    max_2=x;
                }
            }
            return  max_2.key;
        }

答案 1 :(得分:1)

public Key secondMaxKey() {
    if (this.size() <= 1)
        return null;
    if (this.size() == 2){
        if(first.key.compareTo(first.next.key) > 0){
            return first.next.key;
        }
    }
    Key max = first.key;
    Key secondMax = first.next.key;
    Node n = first;
    for (Node x = n.next; x != null; x = x.next) {
        if (x.key.compareTo(max) >= 0) {
            secondMax = max;
            max = x.key;
        } else if (x.key.compareTo(secondMax) > 0)
            secondMax = x.key;
    }
    return secondMax;
}

我相信你的问题在于实施。特别是,将第二个最大值设置为与最大值相同的临时节点

答案 2 :(得分:0)

我认为您应该使用null启动maxmax_2nd并检查if语句中是否为null:

public Key secondMaxKey () {
    Node max = null;
    Node max_2nd = null;

    if (size() < 2) {
        return null;
    }

    for (Node x = first; x != null; x = x.next) {
        if (max == null || x.val.compareTo(max.val) > 0) {
            max_2nd = max;
            max = x;
        } else if (max_2nd == null || x.val.compareTo(max_2nd.val) > 0) {
            max_2nd = x;
        }
    }

    return max_2nd.key;
}

答案 3 :(得分:0)

这对你有帮助,

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        List l=new ArrayList();
        l.add(9);
        l.add(1);
        l.add(2);

        int max2=max_2nd(l);
        System.out.println(max2);
    }
    private static int max_2nd(List l) {

        Set s= new HashSet(l);
        s.remove(Collections.max(s));
        int i =(int)Collections.max(s);
        return i;

    }
}

答案 4 :(得分:0)

请检查以下逻辑。我在这里检查了你的代码并返回第二个最大值(根据问题中的LinkedListST编辑)

   public Key secondMaxKey () {
    Node max = first;
    Node max_2nd = null;
    Node temp = null;

    if (size() < 2) {
        return null;
    }

    for (Node x = first.next; x != null; x = x.next) {
        if (x.key.compareTo(max.key)>0) {
            temp = max;
            max = x;
            if(temp.key.compareTo(max_2nd.key)>0)
            {
                max_2nd=temp;
            }

        } else if (max_2nd == null || x.key.compareTo(max_2nd.key) > 0) {
            max_2nd = x;
        }
    }

    return max_2nd.key;
}

例如

{9,1,2}
Loop 1 (x=1) : max =9 max_2 =0 x=1 After loop max = 9 max_2 = 1
Loop 2 (x=2) : max =9 max_2 =1 x=2 After loop max = 9 max_2 = 2