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.

时间: 2024-10-26 11:45:33

leetcode Intersection of Two Linked Lists python的相关文章

[LeetCode] Intersection of Two Linked Lists

Question: 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. 1.题型分类: 2.思路: 3.时间复杂度:O(n) 4.代

LeetCode——Intersection of Two Linked Lists

Description: 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. 求两个链表的相交的部分 /** * Definitio

[LeetCode] 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. Notes: If the two linked lists have no i

LeetCode—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. Notes: If the two linked lists have no i

[LeetCode] 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. Notes: If the two linked lists have no i

[leetcode]Intersection of Two Linked Lists —— 熟悉python

好久没有耍代码了,手有点生,还有点痒,and 脑子有点锈.话说这个简单的东西,耍了我一个小时. 主要是最近很少碰代码.二是对python不熟悉,刚刚接触,但是顿时很热爱这个小蟒蛇.于是打算好好蹂躏它. 题目链接:https://oj.leetcode.com/problems/intersection-of-two-linked-lists/ 我的思路是这样的. 首先没有循环,所以不用判断是否绕一圈回来了.其次,看题意不会有多次分支,即分支只有一个,且一直相同到尾部.那么好办了. 我只需要逆向判

LeetCode Intersection of Two Linked Lists 解题报告

https://oj.leetcode.com/problems/intersection-of-two-linked-lists/ 求两个链表的第一个公共节点,如果不存在公共节点的话就返回null. A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 解题思路: 1)如果两个链表的最后一个节点一样,那么说明两个链表一定有交点. 2)分别求出两个链表的长度,然后对长度长的链表向前移动:LengA - LengB,将两个链表进行对齐,之后一起遍历,直到找到第一个相同的节

LeetCode Intersection of Two Linked Lists (找交叉点)

题意:给两个链表,他们可能后部分有重叠的地方(重叠是指,两链表都有元素处于同一块地址上),一旦重叠到就不可能分开啦.问在哪重叠的?当然是返回地址了.不重叠就返回null. 思路:用O(n)和O(1)的做法. (1)若两串会重叠,那么到最后一个元素必定会相遇. (2)先判断是否有重叠. (3)有重叠了,要考虑两串长度不一样.后段肯定一样,不重叠的那一段可能不一样.要让它们同长才行,使得两个指针每次只走1步才会相遇. (4)交叉遍历一次就可以使得两个指针处于那样的位置啦.一个指向短链的开头,一个指向

[LeetCode] 160. 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. Notes: If the two linked lists have no i