这是我到目前为止所做的,但我不确定我是否正确地做到了。我无法返回无序链表的maxKey:
public class LinkedListST<Key extends Comparable<Key>, Value> {
private Node first;
private class Node {
private Key key;
private Value val;
private Node next;
public Node(Key key, Value val, Node next) {
this.key = key;
this.val = val;
this.next = next;
}
}
public Key maxKey (Key key) {
Node currentNode = null; //current node we are on
if (first == null)
return null;
for(currentNode = first; currentNode != null; currentNode = currentNode.next){
}
return null;
}
}
答案 0 :(得分:0)
您需要比较所有键并找到最大的
public Key maxKey () {
Node currentNode = null; //current node we are on
if (first == null)
return null;
Key<Comparable> max = first.key;
do {
currentNode = currentNode.next;
if (currentNode == null) break;
if (currentNode.key.compareTo(max) > 0) {
max = currentNode.key;
}
while (true);
return max;
}