Week8 作业
1.判断是否相交
bool isIntersect(ListNode p1, ListNode p2)
{
ListNode* pHead1 = p1;
ListNode* pHead2 = p2;
if (pHead1 == nullptr || pHead2 == nullptr)
{
return false;
}
do
{
if (pHead1 == pHead2)
{
return true;
}
else
{
while (pHead2->pNext != nullptr)
{
pHead2 = pHead2->pNext;
if (pHead1 == pHead2)
{
return true;
}
}
}
pHead1 = pHead1->pNext;
} while (pHead1 != nullptr);
return false;
}
时间复杂度 O(n^2)
空间复杂度 O(1)
2.总结
评论