zl程序教程

您现在的位置是:首页 >  其他

当前栏目

160.相交链表全代码C++

2023-04-18 14:24:29 时间

给你两个单链表的头节点 headAheadB ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 null

图示两个链表在节点 c1 开始相交:
在这里插入图片描述
题目数据 保证 整个链式结构中不存在环。

注意,函数返回结果后,链表必须 保持其原始结构 。

自定义评测:

评测系统 的输入如下(你设计的程序 不适用 此输入):

intersectVal - 相交的起始节点的值。如果不存在相交节点,这一值为 0
listA - 第一个链表
listB - 第二个链表
skipA - 在 listA 中(从头节点开始)跳到交叉节点的节点数
skipB - 在 listB 中(从头节点开始)跳到交叉节点的节点数
评测系统将根据这些输入创建链式数据结构,并将两个头节点 headAheadB 传递给你的程序。如果程序能够正确返回相交节点,那么你的解决方案将被 视作正确答案 。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/intersection-of-two-linked-lists
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

#include <iostream>
using namespace std;
typedef int ElemType;
typedef int Status;
typedef struct LNode
{
    ElemType data;
    struct LNode *next;
} LNode,*LinkedList;

Status InitList(LinkedList &L)
{
    L= new LNode;
    L->next=NULL;
    return 1;
}

void CreatList(LinkedList &L,ElemType n)
{
    L->next=NULL;
    LinkedList r=L;
    for(int i=0; i<n; i++)
    {
        LinkedList p=new LNode;
        cin>>p->data;
        p->next=NULL;
        r->next=p;
        r=p;
    }
}

void InsertList(LinkedList &L, LinkedList &L1, ElemType n,ElemType s)
{
    int a;
    L->next=NULL;
    LinkedList r=L;
    for(int i=0; i<n; i++)
    {
        LinkedList p=new LNode;
        cin>>a;
        if(a != s)
        {
            p->data = a;
            p->next=NULL;
            r->next=p;
            r=p;
        }
        else
        {
            break;
        }
    }
    LinkedList ins;
    ins = L1;
    while(ins->data != s)
    {
        ins = ins->next;
    }
    r->next = ins;
}

void GetList(LinkedList &L)
{
    LinkedList p;
    p=L;
    while(p->next)
    {
        p=p->next;
        cout<<p->data<<" ";
    }
    cout<<endl;
}

int getIntersectionNode(LinkedList &headA, LinkedList &headB)
{
    if(headA == NULL || headB == NULL)
    {
        return -1;
    }
    LinkedList pA = headA;
    LinkedList pB = headB;
    while(pA != pB)
    {
        if(pA == NULL)
        {
            pA = headB;
        }
        else
        {
            pA = pA->next;
        }
        if(pB == NULL)
        {
            pB = headA;
        }
        else
        {
            pB = pB->next;
        }
    }
    if(pA == NULL)
    {
        return -1;
    }
    return pA->data;
}

int main()
{
    LinkedList L1,L2;
    InitList(L1);
    InitList(L2);
    cout<<"输入焦点:"<<endl;
    int s;
    cin>>s;
    if(s == 0)
    {
        cout<<"输入第一个有序链表长度:"<<endl;
        int n1;
        cin>>n1;
        cout<<"正序输入n个元素:"<<endl;
        CreatList(L1,n1);
        cout<<"L1链表为:";
        GetList(L1);
        cout<<endl;
        cout<<"输入第二个有序链表长度:"<<endl;
        int n2;
        cin>>n2;
        cout<<"正序输入n个元素:"<<endl;
        CreatList(L2,n2);
        cout<<"L2链表为:";
        GetList(L2);
    }
    else
    {
        cout<<"输入第一个有序链表长度:"<<endl;
        int n1;
        cin>>n1;
        cout<<"正序输入n个元素:"<<endl;
        CreatList(L1,n1);
        cout<<"L1链表为:";
        GetList(L1);
        cout<<endl;
        cout<<"输入第二个有序链表长度:"<<endl;
        int n2;
        cin>>n2;
        cout<<"正序输入n个元素:"<<endl;
        InsertList(L2,L1,n2,s);
        cout<<"L2链表为:";
        GetList(L2);
    }
    int res = getIntersectionNode(L1, L2);
    cout<<"焦点为:"<<res<<endl;



    return 0;
}