【发布时间】:2018-05-28 14:11:09
【问题描述】:
假设我有一个 1->2->3->4->5 的列表。我想把最后一个节点放在第一个节点之后,所以它就像 1->5->2->3->4。这是我的代码,但它不起作用
public void Manipulate(){
Node curr = head;
Node next = null;
Node last = head;
while(last.next != null){
last = last.next;
}
next = curr.next;
last.next = next;
curr.next = next.next;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
SinglyLinkedList lista = new SinglyLinkedList();
int a = sc.nextInt();
int b = sc.nextInt();
lista.addFirst(a);
lista.insertAfter(a, b);
for(int i = 0; i < 2; i ++){
int c = b;
b = sc.nextInt();
lista.insertAfter(c, b);
}
lista.addLast(34);
lista.addLast(55);
lista.addLast("Ginger");
lista.Manipulate();
System.out.println(lista);
}
【问题讨论】:
-
什么不起作用?
-
抛出异常 - 线程“main”中的异常 java.lang.OutOfMemoryError: Java heap space
-
又是怎么调用的?
-
提供了我的主要方法,你可以从那里检查出来
-
如果您不提供代码,我们应该如何知道
addFirst()、insertAfter()等会发生什么?您是否调试过代码并检查会发生什么? OutOfMemoryError 意味着您正在无限循环中添加元素。
标签: java data-structures singly-linked-list