【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 ...
随机推荐
- 网站部署后Parser Error Message: Could not load type 的解决方案
asp.net 的Webproject 项目是在64bit机上开发,默认选项发布后,部署到32bit的服务器上,出现Parser Error Message: Could not load type的 ...
- 修改ArcGIS Server Account / 站点管理员的 账号、密码
安装ArcGIS Server(10.1之后版本)时会先后创建两个账户:ArcGIS Server Account和站点管理账户 ArcGIS Server Account 是操作系统账户,Serve ...
- setAlpha方法 设置透明度
public void setAlpha (int x) 其中,参数x为透明度,取值范围为0~255,数值越小越透明.
- Web Api 中Get 和 Post 请求的多种情况分析
转自:http://www.cnblogs.com/babycool/p/3922738.html 来看看对于一般前台页面发起的get和post请求,我们在Web API中要如何来处理. 这里我使用J ...
- .NET Framework (代码库、通用类型系统CTS、CLR) 简介
编译C#————>程序集(.exe..dll[MSIL]).元信息[数据信息].可选资源[图片.声音]) | | | ...
- PRAGMA AUTONOMOUS_TRANSACTION
转自 http://blog.csdn.net/pan_tian/article/details/7675800 这段时间遇到一个问题,程序里明明插入了一条记录,但在后边的一段Procedure中却查 ...
- Extjs 中column的renderer使用方法
renderer: function(value, cellmeta, record, rowIndex, columnIndex, store) { if (record.get('productT ...
- 到底为什么你的APP项目烂尾了?
你正在经历迷茫.纠结,或者愤怒.痛苦的情绪,因为,你的APP项目已经或将要烂尾了. 目前的状况只有3种: 项目一直拖到现在,并且很可能继续拖下去 项目在开发期间不断上涨成本 项目完成,BUG多多,不能 ...
- Django 一对多,多对多关系解析
[转]Django 一对多,多对多关系解析 Django 的 ORM 有多种关系:一对一,多对一,多对多. 各自定义的方式为 : 一对一: OneToOneField ...
- [Android Tips] 18. Enable/Disable WiFi via adb
adb shell svc wifi enable|disable Awesome ADB