returns 使用堆栈 Java 的新反向单链表的方法,保留相同的元素但以相反的顺序打印出来
A method which returns a new reveresed Singly Linked List using a stack in Java, keeping the same elements but printing them out in reversed order
例如,如果在包含 2 个元素的列表中,字母 A 是第一个元素,字母 B 是第二个元素,此方法将 return 一个包含相同元素但颠倒的新列表,因此B第一,A第二。不幸的是,在这种情况下它不起作用,我得到的是 EmptyStackException。它还说从未使用过该方法的 return 值,不知道该用什么作为 return 语句 work.Can 有人告诉我我的代码中的错误到底在哪里,或者也许只是指出我正确的方向。提前致谢!
这是我的代码:
public LinkedList<E> reverse() throws EmptyListException {
Stack<LinkedNode<E>> stack = new Stack<>();
LinkedNode<E> temp = head;
while(temp != null){
stack.push(temp);
temp = temp.next;
}
temp = stack.peek();
head = temp;
stack.pop();
while(!stack.isEmpty()){
temp.next = stack.peek();
stack.pop();
temp =temp.next;
}
temp.next = null;
return stack.peek();
}
public static void main(String[] args){
LinkedList<String> List = new LinkedList<>();
List.add("A");
List.add("B");
List.reverse();
-----------------
更新--->
好的,我添加了第二个临时变量,更改了 return 语句并在 main 中使用了 toString() 方法来打印它 out.It 即使有超过 2 个元素也能正常工作,但是当我将鼠标悬停在reverse(),IDE 仍然表示从未使用过该方法的 return 值?!
这是我更新的内容:
LinkedNode<E> temp2 = temp;
while(!stack.isEmpty()){
temp.next = stack.peek();
stack.pop();
temp =temp.next;
}
temp.next = null;
head = temp2;
return temp2;
public static void main(String[] args) {
LinkedList<String> List = new LinkedList<>();
List.add("A");
List.add("B");
List.add("C");
List.add("D");
List.add("E");
List.add("F");
List.reverse();
System.out.println(List.toString());
}
不确定您为什么拥有所有这些代码或尝试使用堆栈。您似乎已经使用了 reverse
库方法,那么为什么不使用它而不是重新发明轮子呢?
LinkedList<String> list = new LinkedList<>();
list.add("A");
list.add("B");
Collections.reverse(list);
还是我没抓住重点?
你不应该return任何东西,你应该在从堆栈中弹出元素时更新温度。类似于:
public void reverse() throws EmptyListException {
if(head == null)
throw new EmptyListException
Stack<LinkedNode<E>> stack = new Stack<>();
LinkedNode<E> temp = head;
while(temp != null){
stack.push(temp);
temp = temp.next;
}
head = stack.peek();
while(!stack.isEmpty()){
temp = stack.peek();
stack.pop();
temp = temp.next;
}
temp.next = null;
}
您的 IDE 抱怨是因为:
List.reverse();
你没有设置reverse方法的return为任何东西,例如:
LinkedNode<E> tmp = List.reverse();
但同样,您不需要return任何东西。
例如,如果在包含 2 个元素的列表中,字母 A 是第一个元素,字母 B 是第二个元素,此方法将 return 一个包含相同元素但颠倒的新列表,因此B第一,A第二。不幸的是,在这种情况下它不起作用,我得到的是 EmptyStackException。它还说从未使用过该方法的 return 值,不知道该用什么作为 return 语句 work.Can 有人告诉我我的代码中的错误到底在哪里,或者也许只是指出我正确的方向。提前致谢!
这是我的代码:
public LinkedList<E> reverse() throws EmptyListException {
Stack<LinkedNode<E>> stack = new Stack<>();
LinkedNode<E> temp = head;
while(temp != null){
stack.push(temp);
temp = temp.next;
}
temp = stack.peek();
head = temp;
stack.pop();
while(!stack.isEmpty()){
temp.next = stack.peek();
stack.pop();
temp =temp.next;
}
temp.next = null;
return stack.peek();
}
public static void main(String[] args){
LinkedList<String> List = new LinkedList<>();
List.add("A");
List.add("B");
List.reverse();
-----------------
更新---> 好的,我添加了第二个临时变量,更改了 return 语句并在 main 中使用了 toString() 方法来打印它 out.It 即使有超过 2 个元素也能正常工作,但是当我将鼠标悬停在reverse(),IDE 仍然表示从未使用过该方法的 return 值?! 这是我更新的内容:
LinkedNode<E> temp2 = temp;
while(!stack.isEmpty()){
temp.next = stack.peek();
stack.pop();
temp =temp.next;
}
temp.next = null;
head = temp2;
return temp2;
public static void main(String[] args) {
LinkedList<String> List = new LinkedList<>();
List.add("A");
List.add("B");
List.add("C");
List.add("D");
List.add("E");
List.add("F");
List.reverse();
System.out.println(List.toString());
}
不确定您为什么拥有所有这些代码或尝试使用堆栈。您似乎已经使用了 reverse
库方法,那么为什么不使用它而不是重新发明轮子呢?
LinkedList<String> list = new LinkedList<>();
list.add("A");
list.add("B");
Collections.reverse(list);
还是我没抓住重点?
你不应该return任何东西,你应该在从堆栈中弹出元素时更新温度。类似于:
public void reverse() throws EmptyListException {
if(head == null)
throw new EmptyListException
Stack<LinkedNode<E>> stack = new Stack<>();
LinkedNode<E> temp = head;
while(temp != null){
stack.push(temp);
temp = temp.next;
}
head = stack.peek();
while(!stack.isEmpty()){
temp = stack.peek();
stack.pop();
temp = temp.next;
}
temp.next = null;
}
您的 IDE 抱怨是因为:
List.reverse();
你没有设置reverse方法的return为任何东西,例如:
LinkedNode<E> tmp = List.reverse();
但同样,您不需要return任何东西。