【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 ...
随机推荐
- in_array支持第三个参数,强制对数据类型检测
in_array函数是判断数据中是否存在指定的内容了,对于这个函数用法非常的简单但在使用过程中会我发现有一些问题. 先介绍一下需求背景: 发票方式: 0=捐赠(不要问我为什么,历史原因) 1=对中寄送 ...
- php课程---数组
数组: 一:定义 1.赋值定义 $arr[0] = 5; $arr[1] = "aa"; print_r ($arr); 2.定义索引数组 $ ...
- [转]从网页Web上调用本地应用程序(.jar、.exe)的主流处理方法
这个方法主要思路是利用自定义URL Protocol来调用应用程序.浏览器在解析到自定义URL Protocol之后,会寻找注册表,然后通过注册表启动相应的程序,然后启动改程序,传入参数.对于我这个项 ...
- Empire C:Basic 1
一.首先,从一个C程序开始: ///Name:Hello World ///Author:JA ///Date:2015-2-4 #include <stdio.h> //包含标准库的信息 ...
- shutter截图工具
安装: 1.打开ubuntu software center,搜索shutter,安装. 使用:
- php执行root命令
一.确定php的sysem等函数可用 二.编写c程序,如ipt.c #include <stdio.h>#include <stdlib.h>#include <sys/ ...
- butterknife异常提示:attribute value must be constant
就是因为你的android工程是lib类型的 如: apply plugin: 'com.android.library' android { compileSdkVersion 23 buildTo ...
- LightOj 1197 - Help Hanzo(分段筛选法 求区间素数个数)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1197 题意:给你两个数 a b,求区间 [a, b]内素数的个数, a and b ( ...
- rabbitMQ集群部署以及集群之间同步
MQ集群部署 期待的部署架构 其中,一个机房有两台机器部署MQ,并组成集群,有一个机房的MQ集群作为中心集群,其他机房的MQ集群将消息同步到中心MQ集群中. 安装erlang,略.. 安装rabbit ...
- video和audio
1.video 使用: <video width="320" height="240" src="http://www.jb51.net/mov ...