Memory limit excess error


  • 0
    B
    public void reorderList(ListNode head) {
            ListNode slow = head;
            ListNode fast = head;
            while(fast!= null && fast.next != null){
                slow = slow.next;
                fast = fast.next.next;
            }
            if (fast != null){
                slow = slow.next;
                fast.next = null;
            }
            fast = head;
            head = fast;
            slow = reverseList(slow);
            while(slow != null){
                ListNode nextSlow = slow.next;
                slow.next = fast.next;
                fast.next = slow;
                fast = slow.next;
                slow = nextSlow;
            }
        }
    
        public ListNode reverseList(ListNode head){
            ListNode newHead = null;
            while (head != null){
                ListNode next = head.next;
                head.next = newHead;
                newHead = head;
                head = next;
            }
            return newHead;
        }
    

    this code gives me memory limit excess error, could anyone tell me why? thanks


  • 0
    A

    You should split List into two, specifically set the member next in previous node of slow to null before call reverseList.


  • 0
    B

    I think slow and fast is already second and the first half of the original list.


  • 0
    G

    I think the system is stricter now. I've done an iterative version, it won't pass with five variable declarations but only allows three, making it almost unreadable to others.


Log in to reply
 

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