LeetCode 160.Intersection of Two Linked Lists.求两个链表的交点

题目:
已知链表A的头节点指针headA,链表B的头节点指针headB,连个链表相交,求两链表交点对应的节点。
要求:
1、如果两个链表没有交点,则返回NULL
2、在求交点的过程中,不可以破坏链表的结构或者修改链表的数据域。
3、可以确保传入的链表A与链表B没有任何环
4、*实现算法尽可能使用时间复杂度O(n),控件复杂度O(1)

思路一:
遍历链表A,将A中节点对应的指针(地址),插入set
遍历链表B,将B中节点对应的指针(地址),在set中查找,
发现在set中的第一个节点指针,及时两个链表的交点。

代码:
使用set的方法,这种方法关键是理解,set中存储的是指针!

//使用set的方法
#include<stdio.h>
#include<iostream>
#include<set>
using namespace std;

struct ListNode{
    int val;
    ListNode* next;
    ListNode(int x):val(x),next(NULL){}
};

//只是定义的时候表名它是指针类型
//传的参数可以直接在方法里面用,以前还在一直纠结
ListNode* getIntersectionNode(ListNode* a,ListNode* b){
    set<ListNode*> node_set; 
    ListNode* headA=a;
    ListNode* headB=b;
    while(headA){
        node_set.insert(headA);
        headA=headA->next;
    }
    while(headB){
        if(node_set.find(headB)!=node_set.end()){
            //set容器会将内容有序化
            return headB;
        }
        headB=headB->next;
    }
    return NULL;
}

int main(){

    ListNode a(1);
    ListNode b(2);
    ListNode c(3);
    ListNode d(4);
    ListNode e(5);
    ListNode f(6);
    ListNode g(7);
    ListNode h(8);
    a.next=&b;
    b.next=&f;
    c.next=&d;
    d.next=&e;
    e.next=&f;
    f.next=&g;
    g.next=&h;
    h.next=NULL;

    //我觉得有问题的是set看样子是可以删除重复元素,并且会使数组内有序,
    //这样一旦是有序之后,怎么会发现第一个相交的结点呢
    //这个问题实际上只用了一个set,就是把a链表的内容放到set中
    // cout<<"检查链表a和链表b"<<endl;
    ListNode* head=&a;
    while(head){
        printf("%d ",head->val);
        head=head->next;
    }
    cout<<endl;
    head=&c;
    while(head){
        printf("%d ",head->val);
        head=head->next;
    }

    cout<<"this is my answer"<<endl;
    ListNode* result=getIntersectionNode(&a,&c);
    printf("%d\n",result->val);

    return 0;
}

不使用set方法:

#include<stdio.h>
#include<iostream>
using namespace std;

struct ListNode{
    int val;
    ListNode* next;
    ListNode(int x):val(x),next(NULL){}
};

//遍历一遍链表就ok
int getLength(ListNode* node){
    int len=0;
    while(node){
        len++;
        node=node->next;
    }
    return len;
}

ListNode* findB(ListNode* node,int n){
    while(node&&n--){
        node=node->next;
    }
    return node;
}

int main(){

    ListNode a(1);
    ListNode b(2);
    ListNode c(3);
    ListNode d(4);
    ListNode e(5);
    ListNode f(6);
    ListNode g(7);
    ListNode h(8);
    a.next=&b;
    b.next=&f;
    c.next=&d;
    d.next=&e;
    e.next=&f;
    f.next=&g;
    g.next=&h;
    h.next=NULL;

    // cout<<"这是长度么";
    // cout<<getLength(&a);
    int len1=getLength(&a);
    int len2=getLength(&c);
    ListNode* other1=&a;
    ListNode* other2=&c;
    if(len1>len2){
        ListNode* ListA=findB(&a,len1-len2);
        while(ListA&&other2){
            if(ListA->val==other2->val){
                printf("%d",other2->val);
                break;
            }
            ListA=ListA->next;
            other2=other2->next;
        }
    }
    else if(len2>len1){
        ListNode* ListB=findB(other2,len2-len1);
        while(ListB&&other1){
            if(ListB->val==other1->val){
                printf("%d",other1->val);
                break;
            }
            ListB=ListB->next;
            other1=other1->next;
        }
    }

    return 0;
}

结果:
在这里插入图片描述

我敲得跟标准答案还是有很大出入的,主要是解题的过程中使用类还是直接使用方法。
在工程中,用类的思想确实能够让代码变得简洁,但是解题的时候,直接用方法会比较好。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值