我正在尝试插入一个请求并按优先级对其进行排序,因此最高(1)位于列表中的第一个。
public Node addByPriority(Object request, int priority) {
size++;
//creates a new node with a priority, owner and creator and sets its next node to the root
Node newNode = new Node(request, priority);
//node to store prev
Node prevNode = null;
//node to store current
Node currNode = first;
//cycle thru the nodes til either the priority is higher or current is null
while (currNode != null && priority >= currNode.getPriority()) {
prevNode = currNode;
currNode = currNode.getNext();
}
if (prevNode == null) {
newNode.setNext(first);
first = newNode;
}
else {
prevNode.setNext(newNode);
newNode.setNext(currNode);
}
// what would be the return statement??
}
它说我需要一个返回语句,但不确定必须输入什么,或者是否有其他方法。
最佳答案
您没有说明您应该返回什么Node
,但按理说您会返回新创建的节点:
return newNode;
关于java - 链表排序插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53251534/