Java 如何反转链表中一定数量的对象?

Java 如何反转链表中一定数量的对象?,java,Java,这是我到目前为止的方法。我不知道该怎么办。我希望该方法采用int参数,表示用户希望在链表中反转多少对象 public void reverseFirstFew(int howMany) // NOT DONE { ListIterator iterator = listIterator(); Object temp = null; if(howMany <= 0){ throw new IndexOutOfBoundsException(); }

这是我到目前为止的方法。我不知道该怎么办。我希望该方法采用int参数,表示用户希望在链表中反转多少对象

public void reverseFirstFew(int howMany) // NOT DONE 
{
   ListIterator iterator = listIterator();
   Object temp = null;

   if(howMany <= 0){
       throw new IndexOutOfBoundsException();
   }
   if(howMany > size()){
       throw new IndexOutOfBoundsException();
   }



}

如果有人能为我指引正确的方向,我将不胜感激

通常一个好的反转解决方案是使用堆栈。 所以我要做的是:

Stack<Object> myStack;
Iterate through my list for as many elements as the user specified (howmany)
     push each element into the stack.

Object JoinHere = the next element in the list;
Now, reconstruct the reversed part of the list using the stack (pop,peek).
When you pop the last element from the stack, make the last element's next    
pointer point to JoinHere.
Stack myStack;
在我的列表中迭代用户指定的元素数(多少个)
将每个元素推入堆栈。
Object JoinHere=列表中的下一个元素;
现在,使用堆栈(pop、peek)重新构建列表的反向部分。
从堆栈中弹出最后一个元素时,使最后一个元素成为下一个元素
指针指向此处。
当然,您应该添加错误检查,例如:比列表大多少…

使用和:

对和都有效

范例

至于为什么这样做,请引用子列表的javadoc:

返回此列表部分的视图,该部分位于指定的
从索引
(包含)和
到索引
(独占)之间。(如果
fromIndex
toIndex
相等,则返回的列表为空。)返回的列表由此列表支持,因此返回列表中的非结构性更改将反映在此列表中,反之亦然。返回的列表支持此列表支持的所有可选列表操作

此方法消除了显式范围操作的需要(通常存在于数组中)。通过传递子列表视图而不是整个列表,任何需要列表的操作都可以用作范围操作。例如,以下习惯用法从列表中删除一系列元素:

list.subList(from, to).clear();

我将给出和安德烈亚斯一样的答案,因为该死的,我把这个编码了,他就在我把答案放在剪贴板上粘贴的时候发布了-p

public class ReverseList
{
   public static void main(String[] args) {
      List<Integer> result = reverseFirst( Arrays.asList( 1,2,3,4,5,6,7,8,9 ), 4 );
      System.out.println( result );
   }

   private static List<Integer> reverseFirst( List<Integer> asList, int howMany )
   {
      List<Integer> sub = asList.subList( 0, howMany );
      Collections.reverse( sub );
      return asList;
   }
}
公共类反向列表
{
公共静态void main(字符串[]args){
列表结果=reverseFirst(Arrays.asList(1,2,3,4,5,6,7,8,9),4);
系统输出打印项次(结果);
}
私有静态列表reverseFirst(列表为列表,整数为多少)
{
List sub=asList.subList(0,数量);
收款。反向(次级);
返回asList;
}
}

哪些项目应该反转?第一个,最后一个,一些其他的组合…?不管用户希望从一开始就有多少。从左到右一种方法是得到一个“多少”的子列表,将整个列表反转,然后将两个列表再次连接在一起。@markspace这是我的想法,但我不确定如何做到这一点如果使用标准的java
列表
,您可以使用
集合。反转
比我快了不到一分钟。这是因为
subList()
将视图返回到父列表中。因此,反转子列表也会反转父列表。经常阅读文档,这会把你搞得一团糟。但这是一个优势,因为我们不必做最后的连接。@Andreas我们必须使用自定义链表,我们不能使用实用程序…这是一个家庭作业assignment@Suds2如果您必须自己实现反转,那么您不能使用此答案,但是如果您的列表正确地实现了
子列表
方法,您可以使用
Collections.reverse()。如果它是一个学校项目(例如数据结构和算法),您需要实现它。然后回信,我可以给你一个样品。否则,如果您想将其作为应用程序的一部分或任何东西使用,那么正如我所说的,其他人可能已经给出了一个很好的解决方案。
List<Integer> list = new LinkedList<>(Arrays.asList(1,2,3,4,5,6,7,8,9));
Collections.reverse(list.subList(0, 4));
System.out.println(list);
list.subList(from, to).clear();
public class ReverseList
{
   public static void main(String[] args) {
      List<Integer> result = reverseFirst( Arrays.asList( 1,2,3,4,5,6,7,8,9 ), 4 );
      System.out.println( result );
   }

   private static List<Integer> reverseFirst( List<Integer> asList, int howMany )
   {
      List<Integer> sub = asList.subList( 0, howMany );
      Collections.reverse( sub );
      return asList;
   }
}