【问题标题】:Why assigning one pointer to another changes original pointer in cpp?为什么将一个指针分配给另一个指针会改变 cpp 中的原始指针?
【发布时间】:2020-06-23 21:04:59
【问题描述】:

假设 headA 指向 [1, 3, 5, 7, 9, 11],而 headB 指向 [2, 4 ,9, 11]。我想找到共同的相交元素problem statement

我不明白为什么 a_pointerb_pointer 最后返回一个空列表。

我关注this教程

算法遵循:(如下)

#include<bits/stdc++.h>


    struct ListNode {
        int val;
        ListNode *next;
        ListNode() : val(0), next(nullptr) {}
        ListNode(int x) : val(x), next(nullptr) {}
        ListNode(int x, ListNode *head) { val = x;  next = head; }
    };


    class Solution {

    public:

        void print(ListNode *head)
        {
            while(head != nullptr)
            {
                printf("%d ->", head->val );
                head = head->next;
            }
            printf("\n");
        }

        
                ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {


                    print(headA);
                    print(headB);

                    ListNode *a_pointer, *b_pointer;
                    a_pointer = headA;
                    b_pointer = headB;

                    while(a_pointer != b_pointer)
                    {
                        

                        if(a_pointer == nullptr)
                        {
                            a_pointer = headB;
                        }
                        else
                        {
                            a_pointer = a_pointer->next;
                        }

                        if(b_pointer == nullptr)
                        {
                            b_pointer = headA;
                        }
                        else
                        {
                            b_pointer = b_pointer->next;
                        }

                    }

                    print(a_pointer);
                    print(b_pointer);
                    return a_pointer;   

                }


            };



    int main()
    {
        Solution s;
        ListNode *node1 = new ListNode(1);
        node1->next = new ListNode(3);
        node1->next->next = new ListNode(5);
        node1->next->next->next = new ListNode(7);
        node1->next->next->next->next = new ListNode(9);
        node1->next->next->next->next->next = new ListNode(11);

        ListNode *node2 = new ListNode(2);
        node2->next = new ListNode(4);
        node2->next->next = new ListNode(9);
        node2->next->next->next = new ListNode(11);

        
        ListNode *ret = s.getIntersectionNode(node1,node2);
        
    }

【问题讨论】:

  • 它不应该改变原来的指针。但是,您可以修改指针指向的内容
  • 在你进入之前可能是空的。
  • 我认为您必须获得并使用调试器。然后设置一个断点,一次执行 1 行代码,查看每一步之后的 4 个变量。
  • 您需要提供一个minimal reproducible example 来证明您的问题。从提供的代码中很难判断您认为哪里有问题。
  • 您的支票不正确。 if (a_pointer == nullptr) 应该是 if (a_pointer-&gt;next == nullptr)b_pointer 检查相同。就像您反复循环遍历两个列表一样,直到两个指针碰巧同时落在其中一个列表的终端 nullptr 上。一旦修复,您的循环将无限运行,因为a_pointer 永远不会等于b_pointer。您应该检查 a_pointer-&gt;val != b_pointer -&gt;val 作为您的循环条件。

标签: c++ pointers linked-list


【解决方案1】:

我不明白为什么赋值 a_pointer = headB 会改变原来的 headB 指针。

不理解为什么会改变原来的headB指针是有道理的,因为原来的headB指针并没有被改变。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多