【leetcode❤python】 160. Intersection of Two Linked Lists
#-*- coding: UTF-8 -*-
#两种方法
#方法1:
#计算出A和B两个链表的长度分别为m、n;
#长度长的链表先走m-n步,之后再一次遍历寻找
#方法2:
#先走到一个链表的尾部,从尾部开始走;
#跳到另一个链表的头部
#如果相遇,则相遇点为重合节点
class Solution(object):
def getLinkLenth(self,head):
lenth=0
while head!=None:
lenth+=1
head=head.next
return lenth
def getIntersectionNode(self, headA, headB):
"""
:type head1, head1: ListNode
:rtype: ListNode
"""
# if headA==None or headB==None:return None
dummyA=ListNode(0)
dummyA.next=headA
dummyB=ListNode(0)
dummyB.next=headB
lenA=self.getLinkLenth(headA)
lenB=self.getLinkLenth(headB)
if lenA>lenB:
xlen=lenA-lenB
for i in xrange(xlen):
dummyA=dummyA.next
if lenB>lenA:
xlen=lenB-lenA
for i in xrange(xlen):
dummyB=dummyB.next
while dummyB.next and dummyA.next:
if dummyB.next==dummyA.next:
return dummyA.next
dummyB=dummyB.next
dummyA=dummyA.next
return None
# listA=[]
listB=[]
if headA==None or headB==None:return None
while headA:
listA.append(headA.val)
headA=headA.next
while headB:
listB.append(headB.val)
headB=headB.next
minlen=len(listA) if len(listA)<len(listB) else len(listB)
print listA,listB,minlen
if listA[-1]!=listB[-1]:return None
for i in xrange(1,minlen+1):
print i
if listA[-i]!=listB[-i]:
return ListNode(listA[-i+1])
if i==minlen:
return ListNode(listA[-i])
【leetcode❤python】 160. Intersection of Two Linked Lists的更多相关文章
- 【LeetCode】160. Intersection of Two Linked Lists 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 栈 日期 题目地址:https://leet ...
- 【一天一道LeetCode】#160. Intersection of Two Linked Lists
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
- 【LeetCode】160. Intersection of Two Linked Lists
题目: Write a program to find the node at which the intersection of two singly linked lists begins. Fo ...
- 【leetcode❤python】350. Intersection of Two Arrays II
#-*- coding: UTF-8 -*- class Solution(object): def intersect(self, nums1, nums2): ...
- 160. Intersection of Two Linked Lists【easy】
160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...
- [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❤python】Sum Of Two Number
#-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100 ...
- 160. 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 ...
随机推荐
- ServletConfig对象和它在开发中的应用场
package cn.itcast; import java.io.IOException; import java.io.PrintWriter; import java.util.Enumerat ...
- 【iCore3 双核心板_ uC/OS-III】例程四:时间管理
实验指导书及代码包下载: http://pan.baidu.com/s/1pKWKuBT iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- Codeforces Round #346 (Div. 2) E F
因为很久没有个人认真做题了 昨天晚上开了场虚拟cf来锻炼个人手速 选的是第一次做cf的场 那时候7出3还被hack...之后也没补题 这次做的时候顺便回忆了一下以前比赛的时候是怎么想的 发现经验还是很 ...
- windows下安装 sphinx 数据库全文搜索引擎
此次演示的环境是:win7系统,64位,php5.4.x,apache sphinx,斯芬克斯(英语不好的同学可以直接读这个音),意狮身人面像 特点:创建索引速度快,3分钟左右能创建100万条记录的索 ...
- JMeter学习-020-JMeter 监听器之【聚合报告】错误率、吞吐量、传输速率实例计算
上文 对聚合报告的结果字段进行了概要的讲述释义,同时对吞吐量.传输速率.分位数等进行了详细的阐述,此文针对上文中描述的吞吐量及传输速率的计算进行详细的实例计算演示. 多不闲述,直入主题! 实际操作步骤 ...
- Git diff 常见用法
Git diff 用于比较两次修改的差异 1.1 比较工作区与暂存区 git diff 比较的是单个仓库的工作区与暂存区的差别,repo diff是对git diff的封装,用来分别显示各个项目 ...
- 如何将SVN patch的修改做成old&new文件
背景 最近解决lua的一则协程问题, 需要将一个patch添加到我们自己的lua库代码中, 由于我们整合的lua库代码目录,与原始的lua库代码不一致,导致不能直接使用path应用到我们自己的lua代 ...
- SQL练习题
create table Student( Sno varchar(20) primary key, Sname varchar(20) not null, Ssex varchar(20) not ...
- Echarts 地图(map)插件之 鼠标HOVER和tooltip自定义提示框
[自行修改 "引号"] 一.鼠标HOVER时的事件: 参照官方文档解释, 可以看出这款插件有丰富的鼠标事件可供选择: 调用鼠标HOVER事件的方法很简单,只需把以下代码放到char ...
- RDIFramework.NET ━ 9.14 数据库连接管理 ━ Web部分
RDIFramework.NET ━ .NET快速信息化系统开发框架 9.14 数据库连接管理 -Web部分 我们经常可以看到很多软件直接把数据库连接字符串放在软件执行目录下的配置文件中,这种直接把 ...