#include<iostream>
#include<stack>
using namespace std;
typedef struct ListNode* Node;
struct ListNode
{
int Value;
Node Next;
};
Node FindFirstCommonNode1(Node list1, Node list2)
{
if(list1 == NULL || list2 == NULL)
{
return NULL;
}
stack<Node> stkFirst;
stack<Node> stkSecond;
while(list1->Next != NULL)
{
stkFirst.push(list1);
}
while(list2->Next != NULL)
{
stkSecond.push(list2);
}
Node lastCommonNode;
while(stkFirst.empty() || stkSecond.empty())
{
if(stkFirst.top() == stkSecond.top())
{
lastCommonNode = stkFirst.top();
stkFirst.pop();
stkSecond.pop();
}
else
{
return lastCommonNode;
}
}
return NULL;
}
Node FindFirstCommonNode2(Node list1, Node list2)
{
if(list1 == NULL || list2 == NULL)
{
return NULL;
}
int length1 =
0;
int length2 =
0;
while(list1->Next != NULL)
{
++length1;
}
while(list2->Next != NULL)
{
++length2;
}
int diff =
abs(length1-length2);
Node longList;
Node shortList;
if(length1 <= length2)
{
longList = list2;
shortList = list1;
}
else
{
longList = list1;
shortList = list2;
}
for(
int i =
0; i < diff; ++i)
{
longList = longList->Next;
}
while(longList != shortList)
{
longList = longList->Next;
shortList = shortList->Next;
}
return longList;
}
转载请注明原文地址: https://ju.6miu.com/read-23492.html