struct Node
{
Node* next;
void* data;
}
const Node* findSameNode(const Node* listA, const Node* listB)
{
//get length
int nLenA = 0;
int nLenB = 0;
Node* node = listA;
while(node != NULL) {
nLenA++;
node = node->next;
}
node = listB;
while(node != NULL) {
nLenB++;
node = node->next;
}
Node* nodeA = listA;
Node* nodeB = listB;
while (nLenA > nLenB)
{
nodeA = nodeA->next;
nLenA--;
}
while (nLenB > nLenA)
{
nodeB = nodeB->next;
nLenB--;
}
while (nodeA != NULL && nodeB != NULL)
{
if (nodeA == nodeB) {
return nodeA;
}
nodeA == nodeA->next;
nodeB == nodeB->next;
}
return NULL;
}
评论