【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 ...
随机推荐
- 创建Mat对象的几种方法
1.Mat的构造函数 Mat M(行数,列数,数据类型,通道数) eg:M(2,2, CV_8UC3, Scalar(0,0,255)). 2.利用Mat的Create()函数.Mat M; M.cr ...
- 小程序和APP谁将主导未来?
APP和小程序的未来会怎么样?小程序的出现真的会加速APP的灭亡吗?今天这篇文章,是对小程序和App未来发展格局的一些思考,更多的是想提醒各位拥抱小程序的的参与者,我们在决定参与这场狂欢的同时,切勿盲 ...
- TTTAttributedLabel xib sb lineSpacing not working
https://github.com/TTTAttributedLabel/TTTAttributedLabel/issues/733 set the same text in storyboard ...
- Oracle交易流水号问题
需求:生成交易流水号,每次新增一条记录时都自动加1,且流水号形式为000000001形式的10位数字. 思路:利用序列可以生成自增的流水号,只需要在前面添加N个0即可,同时,由于数字的长度一定,因此可 ...
- 淘宝分布式文件存储系统:TFS
TFS ——分布式文件存储系统 TFS(Taobao File System)是淘宝针对海量非结构化数据存储设计的分布式系统,构筑在普通的Linux机器集群上,可为外部提供高可靠和高并发的存储访问. ...
- SQL 删除索引错误
SQL Server 数据库执行 ”DROP INDEX 索引名 ON 表名“ 时出现“不允许对索引 '索引名' 显式地使用 DROP INDEX.该索引正用于 PRIMARY KEY 约束的强制执行 ...
- 前端面霸系列(1):doctype 、Quirks Mode & Standards Mode 、document.compatMode
近几日,气压猛降,雾霾铺天盖地,眼看一场腥风血雨就要在前端江湖爆发,这场战争不仅是百度.腾讯.阿狸.搜狐网易新浪等江湖豪门抢夺人才的大战,也是诸位江湖人士重新洗牌的好时机.每年10月,江湖的波动胜过华 ...
- Apache Curator: Zookeeper客户端
Apache Curator Framework url: http://curator.apache.org/curator-framework/ The Curator Framework is ...
- Linux软件包管理
Linux软件包管理 Linux软件包管理主要有2类:是二进制包管理.源码包管理 二进制包管理 主要有RPM和YUM两种 RPM包管理 安装 --ivh:-v ,-vv,-vvv显示的安装信息依次详细 ...
- linux磁盘空间查询
LINUX服务器查询 1. du -sch * 使用该命令查询当前目录下文件夹占用的空间的情况 2. df -hl 查询磁盘剩余空间 3. root权限 fdisk -l