【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 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.
解题思路:
首先这道题目有他的特殊性,这个特殊性就在这里的两个链表如果有交集的话,那么他们在最后面的一定都是相同的。
所以这里催生了两种想法:
从后往前比较,找到最后形同的那个
从前往后比较,但是这里要用到一个技巧。我们首先要去获取到这两个链表的长度,然后让长度长的那个先多走长度差的距离,最后再开始比较,第一个相同的即是。
这里使用了第二种思路:Definition for singly-linked list.
class ListNode:
def init(self, x):
self.val = x
self.next = Noneclass Solution:
@param two ListNodes
@return the intersected ListNode
def getIntersectionNode(self, headA, headB):
ta = ListNode(0)
tb = ListNode(0)
ta = headA
tb = headB
la = 0
lb = 0
while ta != None:
la += 1
ta = ta.next
while tb != None:
lb += 1
tb = tb.next
if la > lb :
ta = headA
tb = headB
tt = la - lb
while tt > 0:
ta = ta.next
tt -= 1
while ta != None and tb != None:
if ta == tb:
return ta
ta = ta.next
tb = tb.next
return Noneelse:
ta = headA
tb = headB
tt = lb -la
while tt > 0:
tb = tb.next
tt -= 1
while ta != None and tb != None:
if ta.val == tb.val:
return ta
ta = ta.next
tb = tb.next
return None
【leetcode】Intersection of Two Linked Lists的更多相关文章
- 【leetcode】Intersection of Two Linked Lists(easy)
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- 【LeetCode】Intersection of Two Linked Lists(相交链表)
这道题是LeetCode里的第160道题. 题目讲的: 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 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 ...
- [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 ...
- 【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 ...
- [leetCode][003] Intersection of Two Linked Lists
[题目]: Write a program to find the node at which the intersection of two singly linked lists begins. ...
- 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 ...
- [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 ...
- 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 ...
随机推荐
- 数据存储_FMDB
一.简单说明 1.什么是FMDB FMDB是iOS平台的SQLite数据库框架 FMDB以OC的方式封装了SQLite的C语言API 2.FMDB的优点 使用起来更加面向对象,省去了很多麻烦.冗余的C ...
- 共有31款PHP 图形/图像处理开源软件(转)
详情点击:http://www.oschina.net/project/lang/22/php?tag=141&os=0&sort=view PHP 图像处理库 Grafika Gra ...
- CDCE913产生任意频率
1,上TI官网下载CDCE913的datasheet和配置软件clock Pro.如果只需要配置CDCE913成某一个固定频率,那么用clock Pro可以很方便快捷. TI的初衷应该就是通过I2C配 ...
- 做为一个前端工程师,是往node方面转,还是往HTML5方面转
文章背景:问题本身来自于知乎,但是我感觉这个问题很典型,有必要把问题在整理一下,重新分享出来. 当看到这个问题之前,我也碰到过很多有同样疑惑的同学,他们都有一个共同的疑问该学php还是nodejs,包 ...
- windows命令
开始--运行--cmd 进入命令提示符 输入netstat -ano 即可看到所有连接的PID 之后在任务管理器中找到这个PID所对应的程序如果任务管理器中没有PID这一项,可以在任务管理器中选&qu ...
- T1加权像(T1 weighted image,T1WI)
T1加权成像(T1-weighted imaging,T1WI)是指这种成像方法重点突出组织纵向弛豫差别,而尽量减少组织其他特性如横向弛豫等对图像的影响. 弛豫:物理用语,从某一个状态恢复到平衡态的过 ...
- Tomcat 知识点
Tomcat(重点) Tomcat是一个符合于Java EE Web标准的最小web容器,所有的jsp程序一定需要有WEB容器的的支持才可以运行,而且在给定的WEB容器里面会支持事务处理操作. Tom ...
- solr基本入门
一直想接触下搜索,虽然之前也玩了下solr,但一直没深入,所以也都忘得差不多了,现在solr都6.1了,发展真快.重新拾起,记录下也好,为以后出问题查找起来快一点. 1.搜索最重要的概念就是倒排索引, ...
- 数据库 'xxx 的事务日志已满。若要查明无法重用日志中的空间的原因,请参阅 sys.databases 中的 log_reuse_wait_desc 列。
---清空日志: USE [master] GO ALTER DATABASE cits SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE cits ...
- JavaScript倒计时
倒计时: 1.设置一个有效的结束日期 2.计算剩余时间 3.将时间转换成可用的格式 4.输出时钟数据作为一个可重用的对象 5.在页面上显示时钟,并在它到达0时停止 <div id="c ...