【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 ...
随机推荐
- GROUP BY 與 Null 值
若群組資料行包含了 Null 值,該資料列將變成結果中的一個群組.若群組資料行內包含了多個 Null 值,Null 值將放入單一群組內.此行為定義於 SQL-2003 標準之中. Product 資料 ...
- 【7集iCore3基础视频】7-3 iCore3硬件介绍
iCore3原理图介绍: 高清源视频:http://pan.baidu.com/s/1gfbhuE3%20密码:xnbc iCore3 购买链接:https://item.taobao.com/ite ...
- 查留言总人数的sql语句
select count(distinct user_id) from bidproduct
- hibernate学习(5)——对象状态与一级缓存
1.对象状态 1.1 状态介绍 hibernate 规定三种状态:瞬时态.持久态.脱管态 瞬时态:transient,session没有缓存对象,数据库也没有对应记录.没有与hibernate关联 ...
- 往sde中导入要素类报错000732
sde可以成功连接,可以在Server中注册. 但是向sde中导入要素类报错000732,如图所示. 点击红色圆圈提示 ERROR 000732. 将路径修改为绝对路径即可,如下图所示.
- class-dump + DumpFrameworks.pl
目的:实现生成 private framework 的 .h,以便倒入项目使用私有 api. 一.class-dump 下载地址: http://stevenygard.com/download/ 安 ...
- LinqToXml
简单的创建一个Xml ///创建一个Xml文档 XElement x = new XElement("qiao");//创建一个根节点 var xx = new XElement( ...
- LeetCode Alien Dictionary
原题链接在这里:https://leetcode.com/problems/alien-dictionary/ 题目: There is a new alien language which uses ...
- SQLServer2005+附加数据库时出错提示操作系统错误5(拒绝访问)错误5120的解决办法
SQLServer2005+ 附加数据库时出错提示操作系统错误5(拒绝访问)错误5120的解决办法 我们在用Sql SQLServer2005+附加数据库文件时弹出错误信息如下图的处理办法: 方案一: ...
- dubbo源码分析2-reference bean发起服务方法调用
dubbo源码分析1-reference bean创建 dubbo源码分析2-reference bean发起服务方法调用 dubbo源码分析3-service bean的创建与发布 dubbo源码分 ...