.判断两个链表是否相交,若相交,求交点。(假设链表不带环)
ListNode *InstersecNode(ListNode *pHead1, ListNode *pHead2)
{
bool flag = IsIntersec(pHead1, pHead2);
if (!flag)
return nullptr;
ListNode *pNode1 = pHead1;
ListNode *pNode2 = pHead2;
while (pNode1&&pNode2)
{
pNode1 = pNode1->_pNext;
pNode2 = pNode2->_pNext;
}
if (pNode1 == nullptr)
pNode1 = pHead1;
else
pNode2 = pHead2;
while (pNode1&&pNode2)
{
if (pNode1 == pNode2)
return pNode1;
pNode1 = pNode1->_pNext;
pNode2 = pNode2->_pNext;
}
return nullptr;
}
bool IsIntersec(ListNode *pHead1, ListNode *pHead2)
{
if (pHead1 == nullptr&&pHead2 == nullptr)
return false;
while (pHead1->_pNext)
pHead1 = pHead1->_pNext;
while (pHead2->_pNext)
pHead2 == pHead2->_pNext;
if (pHead1 == pHead2)
return true;
else
return false;
}
判断两个链表是否相交,若相交,求交点。
见上一篇博客
http://blog.youkuaiyun.com/gjggj/article/details/75213010
请问下面的程序一共输出多少个“-”?
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(void)
{
int i;
for(i=0; i<2; i++){
fork();
printf("-");
}
wait(NULL);
wait(NULL);
return 0;
}
8个
详解见陈浩的博客
http://coolshell.cn/articles/7965.html