Why my code works in IDE with same testcases as in LeetCode, but in LeetCode this code does not work?

huangapple go评论70阅读模式
英文:

Why my code works in IDE with same testcases as in LeetCode, but in LeetCode this code does not work?

问题

The problem on LeetCode:
https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/

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) {}
};

ListNode* removeNthFromEnd(ListNode* head, int n) {
    ListNode **buf = &head;
    for (int i = 0; i < n; ++i) {
        buf = &(*buf)->next;
    }
    (*buf) = (*buf)->next;
    return head;
}

int main() {
    removeNthFromEnd(new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5))))), 2);
    return 0;
}

Runtime Error
Line 18: Char 26: runtime error: member access within null pointer of type 'ListNode' (solution.cpp)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior prog_joined.cpp:27:26

I do not even know what I should try.

英文:

The problem on LeetCode:
https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/

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) {}
};

ListNode* removeNthFromEnd(ListNode* head, int n) {
    ListNode **buf = &amp;head;
    for (int i = 0; i &lt; n; ++i) {
        buf = &amp;(*buf)-&gt;next;
    }
    (*buf) = (*buf)-&gt;next;
    return head;
}


int main() {
    removeNthFromEnd(new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5))))), 2);
    return 0;
}

Runtime Error
Line 18: Char 26: runtime error: member access within null pointer of type 'ListNode' (solution.cpp)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior prog_joined.cpp:27:26

I do not even know what I should try

答案1

得分: 0

您的功能实现与从列表末尾开始删除节点无关。

这个for循环

for (int i = 0; i < n; ++i) {
    buf = &(*buf)->next;
}

从列表的开头计算节点。

而且,当列表包含少于 n 个元素时,它可能会引发未定义行为,因为它使用空指针来访问内存。

您需要使用 delete 运算符删除目标节点。

函数可以如下所示,就像下面的演示程序中所示:

#include <iostream>

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) {}
};

bool removeNthFromEnd(ListNode* &head, size_t n) 
{
    bool success = n != 0;

    if (success)
    {    
        ListNode *last = head;

        while (last && n)
        {
            last = last->next;
            --n;
        }

        if (success = n == 0)
        {
            ListNode **current = &head;

            while (last)
            {
                last = last->next;
                current = &(*current)->next;
            }

            ListNode *tmp = *current;
            *current = (*current)->next;
            delete tmp;
        }
    }

    return success;
}

std::ostream &display(const ListNode *head, std::ostream &os = std::cout)
{
    for (; head != nullptr; head = head->next)
    {
        os << head->val << " -> ";
    }

    return os << "null";
}

int main() 
{
    ListNode *head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5))));

    display(head) << '\n';

    removeNthFromEnd(head, 2);

    display(head) << '\n';

    removeNthFromEnd(head, 4);

    display(head) << '\n';

    removeNthFromEnd(head, 2);

    display(head) << '\n';

    removeNthFromEnd(head, 1);

    display(head) << '\n';

    removeNthFromEnd(head, 1);

    display(head) << '\n';

    return 0;
}

程序的输出是:

1 -> 2 -> 3 -> 4 -> 5 -> null
1 -> 2 -> 3 -> 5 -> null
2 -> 3 -> 5 -> null
2 -> 5 -> null
2 -> null
null
英文:

Your function implementation has nothing common with deleting a node starting from the end of the list.

This for loop

for (int i = 0; i &lt; n; ++i) {
buf = &amp;(*buf)-&gt;next;
}

counts nodes from the beginning of the list.

Also it can invoke undefined behavior when the list contains less than n elements due to using a null pointer to access memory.

And you need to delete the target node using the operator delete.

The function can look the following way as shown in the demonstration program below.

#include &lt;iostream&gt;
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) {}
};
bool removeNthFromEnd( ListNode* &amp;head, size_t n) 
{
bool success = n != 0;
if ( success )
{    
ListNode *last = head;
while (  last &amp;&amp; n )
{
last = last-&gt;next;
--n;
}
if ( ( success  = n == 0 ) )
{
ListNode **current = &amp;head;
while ( last )
{
last = last-&gt;next;
current = &amp;( *current )-&gt;next;
}
ListNode *tmp = *current;
*current = ( *current )-&gt;next;
delete tmp;
}
}
return success;
}
std::ostream &amp; display( const ListNode *head, std::ostream &amp;os = std::cout )
{
for ( ; head != nullptr; head = head-&gt;next )
{
os &lt;&lt; head-&gt;val &lt;&lt; &quot; -&gt; &quot;;
}
return os &lt;&lt; &quot;null&quot;;
}
int main() 
{
ListNode *head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5)))));
display( head ) &lt;&lt; &#39;\n&#39;;
removeNthFromEnd( head, 2);
display( head ) &lt;&lt; &#39;\n&#39;;
removeNthFromEnd( head, 4);
display( head ) &lt;&lt; &#39;\n&#39;;
removeNthFromEnd( head, 2);
display( head ) &lt;&lt; &#39;\n&#39;;
removeNthFromEnd( head, 1);
display( head ) &lt;&lt; &#39;\n&#39;;
removeNthFromEnd( head, 1);
display( head ) &lt;&lt; &#39;\n&#39;;
return 0;
}

The program output is

1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; null
1 -&gt; 2 -&gt; 3 -&gt; 5 -&gt; null
2 -&gt; 3 -&gt; 5 -&gt; null
2 -&gt; 5 -&gt; null
2 -&gt; null
null

huangapple
  • 本文由 发表于 2023年4月4日 05:10:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923794.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定