博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【leetcode 两个链表的交集点】Intersection of Two Linked Lists
阅读量:4137 次
发布时间:2019-05-25

本文共 3289 字,大约阅读时间需要 10 分钟。

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、代码

[cpp] 
  1. ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {  
  2.        ListNode *pa=headA,*pb=headB;  
  3.        int lengthA=0,lengthB=0;  
  4.        while(pa) {pa=pa->next;lengthA++;}  
  5.        while(pb) {pb=pb->next;lengthB++;}  
  6.        if(lengthA<=lengthB){  
  7.            int n=lengthB-lengthA;  
  8.            pa=headA;pb=headB;  
  9.            while(n) {pb=pb->next;n--;}  
  10.        }else{  
  11.            int n=lengthA-lengthB;  
  12.            pa=headA;pb=headB;  
  13.            while(n) {pa=pa->next;n--;}  
  14.        }  
  15.        while(pa!=pb){  
  16.            pa=pa->next;  
  17.            pb=pb->next;  
  18.        }  
  19.        return pa;  
  20.    }  

4、题目作者的解答(英文)
作者双指针法花费的时间比我的方法更少

There are many solutions to this problem:

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

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

  • 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 bi in list B: if bi appears in the hash set, then bi is 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.

    我试了两种双指针方法时间一样,但是作者的思路更新颖~学习!!

转载地址:http://bhovi.baihongyu.com/

你可能感兴趣的文章
linux 2.4.21--->2.4.37 update
查看>>
洞悉linux下的Netfilter&iptables
查看>>
洞悉linux下的Netfilter&iptables --- 2
查看>>
洞悉linux下的Netfilter&iptables ---------- 3
查看>>
洞悉linux下的Netfilter&iptables ------- 4
查看>>
Linux内核2.4和2.6编译模块的方法
查看>>
Linux 2.4和2.6内核模块编译的差别
查看>>
winexec for linux
查看>>
Netfilter框架
查看>>
我和ip_conntrack不得不说的一些事
查看>>
图说adore-ng之端口隐藏
查看>>
进程隐藏
查看>>
Linux下的踪迹隐藏
查看>>
Linux 查看端口使用情况
查看>>
文件隐藏
查看>>
两个linux内核rootkit--之二:adore-ng
查看>>
两个linux内核rootkit--之一:enyelkm
查看>>
关于linux栈的一个深层次的问题
查看>>
rootkit related
查看>>
Linux内核时间获取
查看>>