【发布时间】:2018-09-21 00:52:28
【问题描述】:
我在leetCode中用java写了一段代码,链接如下: https://leetcode.com/problems/reverse-linked-list/description/
它显示“超出内存限制”,谁能解释原因?(您可以将我的代码粘贴到上面的链接中查看错误)
我的代码如下:
public static class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public ListNode reverseList(ListNode head) {
if(head ==null)
return head;
if(head.next ==null){
return head;
}
Stack<ListNode> mStack =new Stack<>();
while(head!=null){
mStack.push(head);
head = head.next;
}
ListNode mNode = new ListNode(0);
ListNode result =mNode;
while(!mStack.empty()){
ListNode temp = mStack.pop();;
mNode.next = temp;
mNode = mNode.next;
}
return result.next;
}
【问题讨论】:
-
鉴于您描述的问题本质上是运行时,因此如果没有任何可运行的代码,很难在此处提供上下文。
-
你可以把我的代码粘贴到上面的leetcode链接中查看结果