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.
 class Solution(object):
def getIntersectionNode(self, headA, headB):
"""
:type head1, head1: ListNode
:rtype: ListNode
""" n = self.getLength(headA)
m = self.getLength(headB) if n < m:
return self.getIntersectionNode(headB, headA) for i in range(n-m):
headA = headA.next while headA and headB:
if id(headA) == id(headB):
return headA
headA = headA.next
headB = headB.next
return None def getLength(self, head):
n = 0
while head:
head = head.next
n += 1
return n
 
1. 总是先操作长度较短的那个list. 
2. 用id(headA) == id(headB) 判断两个node是不是一样

Leetcode 160. Intersection of two linked lists的更多相关文章

  1. [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 ex ...

  2. [LeetCode]160.Intersection of Two Linked Lists(2个链表的公共节点)

    Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...

  3. 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 ex ...

  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 ex ...

  5. Java for 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 ex ...

  6. ✡ leetcode 160. Intersection of Two Linked Lists 求两个链表的起始重复位置 --------- java

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

  7. Java [Leetcode 160]Intersection of Two Linked Lists

    题目描述: Write a program to find the node at which the intersection of two singly linked lists begins. ...

  8. (链表 双指针) 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 ex ...

  9. LeetCode——160 Intersection of Two Linked Lists

    题目 Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3 Output: ...

随机推荐

  1. DEDECMS之七 如何实现文章推荐排行榜

    经常可以看到各种排行榜,这些文章列表的标题之前加了序号,前三条还有显眼样式 1.实现效果 2.实现方法 <ul class="hotPh1"> {dede:arclis ...

  2. noip2008 双栈排序

    题目描述 Description \(Tom\)最近在研究一个有趣的排序问题.如图所示,通过\(2\)个栈\(S_1\)和\(S_2\),\(Tom\)希望借助以下\(4\)种操作实现将输入序列升序排 ...

  3. 招聘 微软全球技术支持中心 sql server组

    微软亚太区全球技术支持中心(APGC CSS)是微软为个人用户.开发者.IT 专业人员到合作伙伴和企业级合作伙伴提供全方位.多元化的服务和技术支持的部门.一个优秀的SQL Server技术支持工程师应 ...

  4. filestream read方法 循环读取固定文件

    1.循环读取啊,byte[]可以定义为1024或者2049等等,不要超过int的maxvalue就可以.然后取出来操作完再去取. FileStream stream = new FileStream( ...

  5. Camera.Parameters 参数

    public class Camera.Parameters extends Object java.lang.Object    ↳ android.hardware.Camera.Paramete ...

  6. [MetaHook] Find a function signature

    Find a non-public function signature, we need a tool "IDA Pro" ( You can open picture in a ...

  7. 开源--豆瓣小组UWP,已上架应用商店

    1.前言 豆瓣小组是我和我老婆都比较喜欢的豆瓣家族里面的一款产品.平时加入了一些小组,偶尔打开看下新鲜的帖子,可以打发一下无聊的时间. 豆瓣小组UWP是我前几周在家里开发的一款windows 10应用 ...

  8. .net 估计要死在你手里了

    最近不太爽,想换工作,上这些知名的招聘网站,一搜 .net 心凉了一截,很少有大公司用.net,工资也不是很高. 不用我多说什么,想必很多人应该有类似经历,只是打了牙往肚子里咽. 来两副图: 最近用滴 ...

  9. js基础知识温习:js中的对象

    在JavaScript中对象是一个无序属性的集合,其属性可以包含基本值.对象或者函数. 对象最简单的创建方式 JavaScript中创建对象最简单的方式就是创建一个Object对象的实例,然后再添加属 ...

  10. JAVA内嵌数据库H2的使用入门

    H2数据库是开源的,非常适合做嵌入式数据库使用,尤其用java编码的时候. H2的优势: 1.h2采用纯Java编写,因此不受平台的限制. 2.h2只有一个jar文件,十分适合作为嵌入式数据库试用. ...