简体   繁体   English

如何递归地在链表中找到最大值?

[英]How to find the maximum value in a linked list recursively?

I need to find the maximum value in a linked list given the head of the list as a parameter recursively. 我需要在列表的头部作为参数递归地找到链表中的最大值。 I have no clue how to start the recursive part of the method. 我不知道如何启动方法的递归部分。 This is all I have so far. 到目前为止,这就是我所拥有的。

int maxOfList(List M){  
    List max = M;
    if(M == null)
        return max;
    if(M.next > max){
        max = M.restOfTheInts;
        return maxOfList();
    }
}

In recursion, you often need an entry method and a worker method. 在递归中,您经常需要一个入口方法和一个worker方法。 The entry method is the special case that allows the worker method to work in all cases, and the worker does the recursion. 进入方法是一种特殊情况,它允许worker方法在所有情况下都可以工作,而worker进行递归。

For example, your worker might be something like: 例如,您的工作人员可能是这样的:

int maxOfList(int currentMax, List<int> listToCheck) {
  // Nothing to compare? currentMax is it!
  if (listToCheck == null || listToCheck.size() == 0) return currentMax;

  // Compare and return.
  List<int> restOfList = listToCheck.subList(1, listToCheck.size());
  return maxOfList(Math.max(currentMax, listToCheck.get(0)), restOfList);
}

And then to kick that off, you need your entry method: 然后要开始,您需要输入方法:

int maxOfList(List<int> listToCheck) {
  return maxOfList(Integer.MIN_VALUE, listToCheck);
}

So, for recursion to effectively work, you need to have the whole context visible inside the function. 因此,对于有效工作的递归,您需要在函数内部显示整个上下文。

int maxOfList(List m) {
     if(m.next == null)
          return m;
     int previousMax = maxOfList(m.next);
     if(m > previousMax)
          return m;
     else
          return previousMax;
}
int maxValue(List m){
   return maxValue(m, Integer.MIN_VALUE);
}

int maxValue(List m, int num){
  if(m.next == null){
     if(m.data > num)
     return num = m.data;
  }
  return maxValue(m.next, num);
}

This should be pretty straightforward. 这应该非常简单。 In order to achieve a recursive solution, think about all these steps: 为了实现递归解决方案,请考虑以下所有步骤:

  • What's the recursive idea? 递归的想法是什么? The maximum of a list {L} is the max(Li, {L} - Li) , where Li is the current element; 列表{L}max(Li, {L} - Li)max(Li, {L} - Li) ,其中Li是当前元素;
  • What's the stop condition? 什么是停止条件? We know that if a list is empty, the maximum could be something that any number will be greater, let's say MIN_INT ; 我们知道如果一个列表是空的,那么最大可能是任何数字都会更大的东西,比方说MIN_INT ;
  • Putting all together: So, at the end we could say that a pseudo-code would look like this: 全部放在一起:所以,最后我们可以说伪代码看起来像这样:

int maxOfList(List M) { if(M == null) return Integer.MIN_VALUE; int max = maxOfList(M.next); return M.value > max ? M.value : max; }

I am supposing that the linked list has its content in value and points to next element in next (tail pointing to null ). 我假设链表有它的内容value和点到下一个元素next (尾指向null )。 If you find any further problems, take a look at this posts: 如果您发现任何其他问题,请查看以下帖子:

Finding Max value in an array using recursion 使用递归查找数组中的最大值

Python: Recursive function to find the largest number in the list Python:递归函数,用于查找列表中的最大数字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM