C++ 控件到达非void函数的末尾

C++ 控件到达非void函数的末尾,c++,C++,我正在做一个关于Hackerrank的问题,但每当我编译代码时,它都会显示控制权在非void函数的末尾到达。以下是我的源代码: /* Compare two linked lists A and B Return 1 if they are identical and 0 if they are not. Node is defined as struct Node { int data; struct Node *next; } */ int CompareLists(Node

我正在做一个关于Hackerrank的问题,但每当我编译代码时,它都会显示控制权在非void函数的末尾到达。以下是我的源代码:

   /*
Compare two linked lists A and B
Return 1 if they are identical and 0 if they are not. 
Node is defined as 
struct Node
{
 int data;
 struct Node *next;
}
*/
int CompareLists(Node *headA, Node* headB)
{

if(headA==NULL&&headB==NULL)
 {
   return 1;
 }
 else if( headA!=NULL&&headB!=NULL)
 {
   while(headA!=NULL&&headB!=NULL)
    {
       if(headA->data==headB->data)
           {
           headA=headA->next;
           headB=headB->next;
             }
       else
           {
           return 0;
           exit (0);
       }
       return 1;
   }
}
else
    {
    return 0;
}
}

请告知如何更正此错误,并提前感谢。

执行此代码后会发生什么

if(headA->data==headB->data)
{
     headA=headA->next;
     headB=headB->next;
}

…如果headA->next==NULL或headB->next==NULL?

我可以在这里看到两个关于可达性的问题。首先是一个简单的问题:

  {
      return 0;
      exit (0);
  }
无法访问退出呼叫。这句话几乎肯定是个错误。我想不出什么好的理由打电话给出口

接下来是更复杂的一个。。。这是编译错误的根本原因:

while(headA!=NULL&&headB!=NULL)
    {
       if(headA->data==headB->data)
           {
           headA=headA->next;
           headB=headB->next;
             }
       else
           {
           return 0;
           exit (0);
       }
       return 1;
   }
看哪里有退货1;是它在循环中

那么,如果headA!=空和头B!=NULL的计算结果为false?在这种情况下,带有回路1的回路体;最后跳过了。。。你就到了方法的尽头

因此出现了编译错误


我怀疑修复方法是在循环后将返回移动到,但我没有尝试理解代码的逻辑,因此这可能是错误的修复方法。

如果修复缩进,可能会帮助您了解出了什么问题。