我目前是一名 Comp sci 大一学生,致力于通过课堂和在线学习数据结构。
我也是新手,但过去对我有很大帮助。
我当前的问题是搜索 LinkedList 以返回其编号出现在列表中的最后一个索引。
我觉得这与递归和不断搜索有关,直到您可以以某种方式检查确定这是该项目的最后一次出现,然后返回其索引。
然而,我的第一学期 Java 课程根本没有涉及递归,我有点不知所措。
在这些课程中,我并不要求明确的答案,我只需要一些指导。或者向我保证我在研究递归方面走在正确的道路上?
另外,这是我到目前为止所尝试的。谢谢您的帮助!
public int getLastIndexOf(int lastItem) { //goal is to return the lastindex at which that number appears last
Node current;
current = head;
int count = 0; //count variable
while (current.next != null) { //go through list
if (current.dataItem == lastItem) {
//check rest of the list for if that number exists beyond
}
count++; //increment count for every time loop executes
current = current.next; //moves onto next node to check
}
return -1;
}