#-*- 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的更多相关文章

  1. 【LeetCode】160. Intersection of Two Linked Lists 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 栈 日期 题目地址:https://leet ...

  2. 【一天一道LeetCode】#160. Intersection of Two Linked Lists

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...

  3. 【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 ...

  4. 【leetcode❤python】350. Intersection of Two Arrays II

    #-*- coding: UTF-8 -*- class Solution(object):    def intersect(self, nums1, nums2):                ...

  5. 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 ...

  6. [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 ...

  7. [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 ...

  8. 【leetcode❤python】Sum Of Two Number

    #-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100 ...

  9. 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 ...

随机推荐

  1. 在python 中is和= = 的区别

    Python中的对象包含三要素:id.type.value其中id用来唯一标识一个对象,type标识对象的类型,value是对象的值is判断的是a对象是否就是b对象,是通过id来判断的==判断的是a对 ...

  2. Matlab图像处理函数:regionprops

    本篇文章为转载,仅为方便学术讨论所用,不用于商业用途.由于时间较久,原作者以及原始链接暂时无法找到,如有侵权以及其他任何事宜欢迎跟我联系,如有侵扰,在此提前表示歉意.----------------- ...

  3. HDU 5416 CRB and Tree(前缀思想+DFS)

    CRB and Tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  4. phpcms流程

    phpcms流程1: 安装 将下载好的文件放到www目录下 地址栏中输入 http://localhost/phpcms/install_package/install 打开安装页面 进行安装即可. ...

  5. Xcode 杂七杂八

    一.Exception 的捕捉 1.message send to dealloc instance  a, 输出控制台(lldb)后面输入:c + enter, 找到对应的行      b, po ...

  6. 从oracle数据表中读取表结构

    drop table aa_tab;create table aa_tab asselect  A.TABLE_NAME  表名,  A.column_name 字段名,A.data_type  数据 ...

  7. Maven-005-部署构件至 nexus 私服

    nexus 私服仓库中宿主仓库主要用于储存装置内部的或一些无法从公共仓库获取的第三方构件,供项目组的人员使用.日常开发中,可将各版本构件直接部署到 Nexus 中对应策略的宿主仓库中.上篇文章中讲述了 ...

  8. mysql 三种恢复方式

    为了保障数据的安全,需要定期对数据进行备份.备份的方式有很多种,效果也不一样.一旦数据库中的数据出现了错误,就需要使用备份好的数据进行还原恢复.从而将损失降到最低.下面我们来了解一下MySQL常见的有 ...

  9. Java 类加载机制

    类的加载: 类的初始化: 类什么时候才被初始化:1)创建类的实例,也就是new一个对象2)访问某个类或接口的静态变量,或者对该静态变量赋值3)调用类的静态方法4)反射(Class.forName(&q ...

  10. 一个iOS项目中包含多个xcodeproj文件,如何运行其中的一个项目

    从GitHub上下载的Masonry的iOS源码,打开发现有多个项目,直接运行,模拟器没反应.由于Masonry是一个多工程的项目,每个项目都依赖Masonry的源码,所以要运行Masonry的Exa ...