判断链表是否有交点若有找出交点

来源:互联网 发布:免费下载绘图软件 编辑:程序博客网 时间:2024/05/18 03:14

.判断两个链表是否相交,若相交,求交点。(假设链表不带环)

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.csdn.net/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

原创粉丝点击