zoukankan      html  css  js  c++  java
  • leetcode Intersection of Two Linked Lists python

    Intersection of Two Linked Lists

    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.

    python code:

    # Definition for singly-linked list.
    # class ListNode:
    # def __init__(self, x):
    # self.val = x
    # self.next = None

    class Solution:
    # @param two ListNodes
    # @return the intersected ListNode
    def getIntersectionNode(self, headA, headB):
      if not headA or not headB:      #边界条件判定
        return None
      else:
        p={}
        i=headA
        while i:              #遍历其中一个列表,得到一个dict(hash table)
          p[i.val]=1
          i=i.next
        i=headB
        while i:
          if i.val in p:          #遍历另一个列表,如果某个元素在上面得到的dict中,则返回此元素
            return i
          else:
            i=i.next
         return None

    other solutions given by leetcode:

    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.

    Analysis written by @stellari.

  • 相关阅读:
    充满了艰难的造题之幼儿园测试【历史考试】
    【大型网站技术实践】初级篇:借助Nginx搭建反向代理服务器
    windows下nginx的安装使用及解决80端口被占用nginx不能启动的问题
    使用hMailServer搭建免费邮件服务器(亲测可用)
    $.ajax()方法详解
    详细解读Jquery各Ajax函数:$.get(),$.post(),$.ajax(),$.getJSON()
    $.ajax()调用.cs里的方法实现ajax操作示例
    Asp.Net URL重写的具体实现(通过URLRewriter.dll)
    数据图
    C#解析JSON字符串总结
  • 原文地址:https://www.cnblogs.com/bthl/p/4574517.html
Copyright © 2011-2022 走看看