为什么我的链表方法'反转()'抛出异常OutOfMemoryError?


package com.company;
import java.util.ArrayList;
class Node{
public Node(int val, Node next){
this.val = val;
this.next = next;
}
public Node(int val){
this(val, null);
}
int val;
Node next;
private void setVal(int newVal){
this.val = newVal;
}
private void setNext(Node newNextNode){
this.next = newNextNode;
}
}
public class MyCodeLink {
private Node head;
private int size;
public MyCodeLink(int val){
this.head = new Node(val, null);
this.size = 1;
}
public void insert(int index, int val){
if (index < 0 || index > this.getSize()){
throw new IndexOutOfBoundsException("index must >= 0 and <= size");
}
if (index == 0){
this.head = new Node(val, head);
this.size++;
return;
}

Node cur = head;
for (int i = 0; i < index - 1; i++){
cur = cur.next;
}
Node node = new Node(val, cur.next);
cur.next = node;
this.size++;
}
public void insertToHead(int val){
insert(0, val);
}
public void insertToLast(int val){
insert(this.getSize(), val);
}
public int getSize(){
return this.size;
}
public Node getHead(){
return head;
}
@Override
public String toString(){
StringBuilder s = new StringBuilder();
Node cur = head;
while (cur != null){
s.append(cur.val).append("t");
cur = cur.next;
}
return s.toString();
}
public void reversal(){
//        they will throw `java.lang.OutOfMemoryError: Java heap space` too!
//        Node cur = this.head;
//        ArrayList<Node> stack = new ArrayList<Node>();
//
//        while (cur.next != null){
//            stack.add(cur);
//            cur = cur.next;
//        }
//
//        this.head = cur;
//
//        while (!stack.isEmpty()){
//            cur.setNext(stack.remove(stack.size() - 1));
//            cur = cur.next;
//        }
Node p1 = this.head;
Node p2 = p1.next;
while (p2 != null){
Node temp = p2.next;
p2.next = p1;
p1 = p2;
p2 = temp;
}
this.head = p1;
}
public static void main(String[] args){
MyCodeLink myCodeLink = new MyCodeLink(8);
System.out.println("size: " + myCodeLink.getSize());
System.out.println(myCodeLink);
myCodeLink.insertToHead(6);
System.out.println("size: " + myCodeLink.getSize());
System.out.println(myCodeLink);
myCodeLink.insert(1, 7);
System.out.println("size: " + myCodeLink.getSize());
System.out.println(myCodeLink);
myCodeLink.insertToLast(9);
System.out.println("size: " + myCodeLink.getSize());
System.out.println(myCodeLink);
myCodeLink.reversal();
System.out.println("size: " + myCodeLink.getSize());
System.out.println(myCodeLink);
}
}
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.base/java.util.Arrays.copyOf(Arrays.java:3537)
at java.base/java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:228)
at java.base/java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:829)
at java.base/java.lang.StringBuilder.append(StringBuilder.java:253)
at com.company.MyCodeLink.toString(MyCodeLink.java:74)
at java.base/java.lang.String.valueOf(String.java:4218)
at java.base/java.io.PrintStream.println(PrintStream.java:1047)
at com.company.MyCodeLink.main(MyCodeLink.java:132)

我正在学习链表,当我实现反向链表时出现了这个问题

我尝试用其他方式实现反转方法,它们已被注释。

这段代码发生了什么?

我也试着用其他版本的Java编译这段代码,结果是相同的

这应该是我的问题,但我真的不明白为什么

我已经在评论中概述了这个问题:您的反转实现是错误的,您最终将最初的第一个和第二个元素彼此指向。

我建议选择比p1p2更好的元素,以明确您当前正在处理的元素。下面的代码可能会更清楚

Node current = this.head; // we start at the head
Node previous = null; // and the previous of head should be null after the reversal
while (current != null){
Node temp = current.next; // save the next node
current.next = previous; // point the current node back instead of forward
previous = current; // set the new previous node
current = temp; // move one forward
}
this.head = previous; // current is null by now

相关内容

  • 没有找到相关文章

最新更新