Why does my solution fail memory limit?


  • 0
    A
    public class Solution {
        public ListNode oddEvenList(ListNode head) {
            if(head == null){
                return null;
            }
            
            int count = 1;
            ListNode oh = null;
            ListNode eh = null;
            
            ListNode temp = head;
            ListNode ot = null;
            ListNode et = null;
            
            while(temp != null){
                if(count % 2 == 0){
                    if(eh == null){
                        eh = temp;
                        et = eh;
                    }
                    else{
                        et.next = temp;
                        et = temp;
                    }
                }
                else{
                    if(oh == null){
                        oh = temp;
                        ot = oh;
                    }
                    else{
                        ot.next = temp;
                        ot = temp;
                    }
                }
                temp = temp.next;
                count++;
            }
            
            ot.next = eh;
            
            return oh;
        }
    }

  • 1
    E

    I think creating the four empty nodes exceeds the memory limit; even though it technically is constant, it must violate small cases like having a list with just three nodes


  • 0
    A

    Thanks for the insight :).


  • 0
    F

    Please note here we are talking about the node number and not the value in the nodes.


Log in to reply
 

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