Why does My solution show "Memory Limit Exceeded "?


  • 0
    X
    public class Solution {
    public ListNode reverseList(ListNode head) {
        if(head == null) return head;
        ListNode point = head.next;
        ListNode tmp;
        while(point!=null){
            tmp = point;
            point = point.next;
            
            tmp.next = head;
            head = tmp;
        }
        return head;
    }
    

    }


  • 0
    C

    I ran into this same issue. Right now there's a loop when you follow the 'next' path between the original first and second element -- the two elements' 'next' variables point to each other.


  • 1
    K

    In the "tmp = point", you make "tmp" points to head.next.
    then in the "tmp.next = head", you make "tmp.next" points to head.
    So we have a circle linked list with two elements .
    And I think OJ calls this situation as " MLE " during testing the output.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.