【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 ...
随机推荐
- @Column
@Column标记表示所持久化属性所映射表中的字段,该注释的属性定义如下: @Target({METHOD, FIELD}) @Retention(RUNTIME) public @interface ...
- MyEclipse部署web项目到Tomcat出现An internal error occurred during: "Launching on Tomcat 7.x"的问题
如果出现了上述的错误按照如下的3个步骤解决:1.首先关闭MyEclipse工作空间.2.然后删除工作空间下的文件."MyEclipse10\workspace.metadata.plugin ...
- ubuntu下非root用户下获得使用wireshark的权限
在非root用户下不能使用wireshark用来抓包,所以需要进行以下操作: sudo groupadd wireshark sudo chgrp wireshark /usr/bin/dumpcap ...
- 在Main方法中设置异常的最后一次捕捉
在做Winfrom程序时,有时会遇到一个异常,可是这个异常不知道在什么地方发生的,程序会自动关闭,然后什么也没有了,在网上找到了一种方法,用来捕捉这种异常. 出现这种情况的原因是在程序中某些地方考虑不 ...
- Linux启动界面切换:图形界面-字符界面(转)
Linux字符界面切换到图形界面 由字符界面切换到图形界面可用两种简单方法实现: 1.在字符界面输入startx或init 5 . 2.通过编辑/etc/inittab文件实现默认进入图形界面. 把其 ...
- 阿里笔试题:在n个人中找明星
题目描述:有N个人,其中一个明星和n-1个群众,群众都认识明星,明星不认识任何群众,群众和群众之间的认识关系不知道,现在如果你是机器人R2T2,你每次问一个人是否认识另外一个人的代价为O(1),试设计 ...
- RabbitMQ之window安装步骤
安装Rabbit MQ Rabbit MQ 是建立在强大的Erlang OTP平台上,因此安装Rabbit MQ的前提是安装Erlang.通过下面两个连接下载安装3.2.3 版本: 下载并安装 Era ...
- CH模拟赛 拆地毯
/* MST,注意只能加K条边,但是备选是M条边 */ #include<iostream> #include<cstdio> #include<string> # ...
- Block使用
1.对block的理解 > block是iOS4.0之后出现的,是仿照java中匿名函数所创造的,它是c级别的语法,效率比协议-代理高 > block的是一个匿名函数(没有名字的函数) ...
- kali安装vmtools问题
切记使用此法,一定要确保kali没有装过vmware workstation自带的vmware_tools,不然要卸载之后才能使用.我当初就是安装了后使用此法,不能成功,卸载也不行,导致完全重装 安装 ...