Skip to content

Bug Report for Memory Consumption #5532

@mariopavlov

Description

@mariopavlov

Bug Report for https://neetcode.io/problems/reverse-a-linked-list

Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.

Having the following solution on my end:

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
		ListNode* prev = nullptr;
		auto cur = head;

		while (cur) {
			auto temp = cur->next;

			cur->next = prev;

			prev = cur;
			cur = temp;
		}

		return prev;
    }
};

I have received 148 MBs from the grading system. Here is the exact report:

Memory: 148.1 MB
Time: 2ms

Submitted at: 02/27/2026 17:09

Then I saw that there are better submissions in terms of memory allocation, and I checked the code:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if(!head || !head->next)return head;

        ListNode* prev=NULL;
        ListNode* node=head;

        while(node!=NULL){
            ListNode* front=node->next;
            node->next=prev;
            prev=node;
            node=front;
        }
        // head=node;
        return prev;
    }
};

For that submission we have the following: Runtime: 2ms Memory: 1.1 MB.

How is it possible that similar solutions have such big difference in Memory allocation? Given that I use auto, which is compiler feature? Am I missing something here, or there is a bug in the grading and measuring process?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions