Intersection of Two Linked Lists

Intersection of Two Linked Lists

1、题目

Write a program to find the node at which the intersection of two singly linked lists begins.

For example, the following two linked lists:

A:          a1 → a2
                   ↘
                     c1 → c2 → c3
                   ↗            
B:     b1 → b2 → b3

begin to intersect at node c1.

Notes:

  • If the two linked lists have no intersection at all, return  null.
  • The linked lists must retain their original structure after the function returns.
  • You may assume there are no cycles anywhere in the entire linked structure.
  • Your code should preferably run in O(n) time and use only O(1) memory.

2、分析

题目大意:给两个链表,找出它们交集的那个节点,要求时间复杂度O(n),空间复杂度O(1)。

有以下几种思路:

(1) 暴力破解 ,遍历链表A的所有节点,并且对于每个节点,都与链表B中的所有节点比较,退出条件是在B中找到第一个相等的节点。时间复杂度O(lengthA*lengthB),空间复杂度O(1)。

(2) 哈希表 。遍历链表A,并且将节点存储到哈希表中。接着遍历链表B,对于B中的每个节点,查找哈希表,如果在哈希表中找到了,说明是交集开始的那个节点。时间复杂度O(lengthA+lengthB),空间复杂度O(lengthA)或O(lengthB)。

(3) 双指针法 ,指针pa、pb分别指向链表A和B的首节点。

遍历链表A,记录其长度lengthA,遍历链表B,记录其长度lengthB。

因为两个链表的长度可能不相同,比如题目所给的case,lengthA=5,lengthB=6,则作差得到 lengthB- lengthA=1,将指针pb从链表B的首节点开始走1步,即指向了第二个节点,pa指向链表A首节点,然后它们同时走,每次都走一步,当它们相等时,就是交集的节点。

时间复杂度O(lengthA+lengthB),空间复杂度O(1)。双指针法的代码如下:

3、代码

ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
    ListNode *pa=headA,*pb=headB;
    int lengthA=0,lengthB=0;
    while(pa) {pa=pa->next;lengthA++;}
    while(pb) {pb=pb->next;lengthB++;}
    if(lengthA<=lengthB){
      int n=lengthB-lengthA;
      pa=headA;pb=headB;
      while(n) {pb=pb->next;n--;}
    }else{
      int n=lengthA-lengthB;
      pa=headA;pb=headB;
      while(n) {pa=pa->next;n--;}
    }
    while(pa!=pb){
      pa=pa->next;
      pb=pb->next;
    }
    return pa;
  }
4、题目作者的解答(英文)

作者双指针法花费的时间比我的方法更少

There are many solutions to this problem:

  • Brute-force solution (O(mn) running time, O(1) memory):

    For each node a iin list A, traverse the entire list B and check if any node in list B coincides with a i.

  • Hashset solution (O(n+m) running time, O(n) or O(m) memory):

    Traverse list A and store the address / reference to each node in a hash set. Then check every node b iin list B: if b iappears in the hash set, then b iis the intersection node.

  • Two pointer solution (O(n+m) running time, O(1) memory):
    • Maintain two pointers pA and pB initialized at the head of A and B, respectively. Then let them both traverse through the lists, one node at a time.
    • When pA reaches the end of a list, then redirect it to the head of B (yes, B, that's right.); similarly when pB reaches the end of a list, redirect it the head of A.
    • If at any point pA meets pB, then pA/pB is the intersection node.
    • To see why the above trick would work, consider the following two lists: A = {1,3,5,7,9,11} and B = {2,4,9,11}, which are intersected at node '9'. Since B.length (=4) < A.length (=6), pB would reach the end of the merged list first, because pB traverses exactly 2 nodes less than pA does. By redirecting pB to head A, and pA to head B, we now ask pB to travel exactly 2 more nodes than pA would. So in the second iteration, they are guaranteed to reach the intersection node at the same time.
    • If two lists have intersection, then their last nodes must be the same one. So when pA/pB reaches the end of a list, record the last element of A/B respectively. If the two last elements are not the same one, then the two lists have no intersections.


可以将A,B两个链表看做两部分,交叉前与交叉后。

交叉后的长度是一样的,因此交叉前的长度差即为总长度差。

只要去除这些长度差,距离交叉点就等距了。

为了节省计算,在计算链表长度的时候,顺便比较一下两个链表的尾节点是否一样,

若不一样,则不可能相交,直接可以返回NULL


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值