小编Nit*_*hod的帖子

Java中LinkedList的异常行为

我试图解决需要使用的“黑客等级”问题LinkedList,但发现了一些奇怪的问题。目的是打印LinkedList反面。

我已经尝试调试程序,但是找不到任何错误。

在下面的第一段代码中,我只能将的第一个和最后一个元素LinkedList放入ArrayList

static void reversePrint(SinglyLinkedListNode head) {
    List<Integer> tempList = null;

    if (head == null)
        return;
    else {
        tempList = new ArrayList<>();
        tempList.add(head.data);
        while(head.next != null)
            head = head.next;
        tempList.add(head.data);
    }
    System.out.println("Size of the List -"+tempList.size());
    for(int i = tempList.size()-1; i >= 0; i--)
        System.out.println("Index +"+i+" "+tempList.get(i));
}
Run Code Online (Sandbox Code Playgroud)

在下面的代码中,我将java.lang.OutOfMemoryError: Java heap space无法理解实际上是什么导致了此错误。

static void reversePrint(SinglyLinkedListNode head) {
    List<Integer> tempList = null;

    if (head == null)
        return;
    else {
        tempList …
Run Code Online (Sandbox Code Playgroud)

java linked-list data-structures

0
推荐指数
1
解决办法
60
查看次数

标签 统计

data-structures ×1

java ×1

linked-list ×1