Using the parameter takes more time than using a copy of it?

  • 0

    Here is my code, which runs in 16ms.

       void deleteNode(ListNode* node) {
            ListNode* current = node;
            ListNode* next_temp = current -> next;
            current -> val = next_temp -> val;
            current -> next = next_temp -> next;

    It's not the best, but here is a question: If I change this code slightly:

       void deleteNode(ListNode* node) {
            ListNode* next_temp = node -> next;
            node -> val = next_temp -> val;
            node -> next = next_temp -> next;

    By directly using the parameter "node" instead of "current", the time becomes 20ms. Why? Could it be because a local variable is put in a register, but parameters are in memory, or caches at best?

Log in to reply

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