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. PHP命令行模式

    <?php error_reporting(E_ALL); header('Content-Type:text/plain;charset=utf-8'); interface CommandA ...

  2. (转)c# 解析JSON的几种办法

    来自:http://blog.csdn.net/gaofang2009/article/details/6073029 欲成为海洋大师,必知晓海中每一滴水的真名. 刚开始只是想找一个转换JSON数组的 ...

  3. Linux 网络编程详解十二

    UDP的特点 --无连接 --基于消息的数据传输服务 --不可靠 --UDP更加高效 UDP注意点 --UDP报文可能会丢失,重复 --UDP报文可能会乱序 --UDP缺乏流量控制(UDP缓冲区写满之 ...

  4. Linux设置环境变量(解决许多命令找不到)

    不知道服务器被谁给改坏了,许多命令都不能使用找不到,但是可以在/usr/bin/,/usr/local/bin等里面找到源程序,当时首先想到的就是环境变量,因为Windows在设置了环境变量之后就可以 ...

  5. "org.jboss.netty.internal.LoggerConfigurator".DESCRIBED is already registered 的解决办法

    今天在jboss 6.2 EAP上部署一个项目时,报以下错误: org.jboss.msc.service.DuplicateServiceException: Service jboss.pojo. ...

  6. 在线程中调用SaveFileDialog

    在多线程编程中,有时候可能需要在单独线程中执行某些操作.例如,调用SaveFileDialog类保存文件.首先,我们在Main方法中创建了一个新线程,并将其指向要执行的委托SaveFileAsyn.在 ...

  7. 正则表达式语法(msdn)

    “正则表达式”描述在搜索文本正文时要匹配的一个或多个字符串.该表达式可用作一个将字符模式与要搜索的字符串相匹配的模板. 正则表达式包括普通字符(例如,a 到 z 之间的字母)和特殊字符(称为“元字符” ...

  8. 没有jquery的时候,你看看这个

    vjs var br = (function() { var ua = navigator.userAgent.toLowerCase(); browser = { iPhone: /iphone/. ...

  9. [BZOJ 2819]NIM(dfs序维护树上xor值)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2819 分析: 树上的nim游戏,关键就是要判断树上的一条链的异或值是否为0 这个题目有 ...

  10. Matlab 的reshape函数

    看Matlab的help文档讲得不是清楚. 先给上一段代码: >> a=[1 2 3;4 5 6;7 8 9;10 11 12]; >> b=reshape(a,2,6); 这 ...