从链表中删除最小值 [英] Remove minimum value from linkedlist

查看:103
本文介绍了从链表中删除最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从链表中删除最小的元素值。像

I need to remove the smallest element value from the linked list. Like

{8,4,7,2,9,4,5,3} 

成为:

{8,4,7,9,4,5,3}

我写了:

public void RemoveMin() {
    T min = list.getInfo();
    for (int i = 0; i < 7; i++) {
        if (list.getLink() < min)
            min = (T) list.getLink();
        else
            continue;
    }
} 


推荐答案

你可以这样做:

public void RemoveMin()
{
   int nbElements = list.size()
   int lowerValueIndex = 0
   for (i = 0; i < nbElements; i++)
   {
      if(list.get(i) < list.get(lowerValueIndex)
      {
         lowerValueIndex = i;
      }
   }
   list.remove(lowerValueIndex);
}

首先,使用第一个元素初始化最低的值
然后在每个元素中,将值与已经找到的较低值进行比较
如果新的是较低的,则采取新的lowerValueIndex
最后你只需要删除最低的元素
要小心,使用这个解决方案,你的数据必须是可比的直接

First you initializethe lowest value with the first element. Then at each element, you compare the value with the lower one who is already find. if the new is lower, you take the new lowerValueIndex. At the end you just have to remove the lowest element founded. Be carefull, with this solution, your data must be comparable directly

这篇关于从链表中删除最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆