#-*- 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. java中类的继承

    我们都知道java的四大特性:抽象.继承.封装.多态: 那么关于继承有哪些特性呢?下面看具体实例: (1) public class Person { public  int a=2; public  ...

  2. IOS彩票第二天设置界面(2)

    *********代码的抽取ILBaseTableViewController.h #import <UIKit/UIKit.h> @interface ILBaseTableViewCo ...

  3. P4 前端编译器p4c-bm、后端编译器bmv2命令安装 make error问题

    参考:Github 安装p4c-bm: sudo pip install -r requirements.txt sudo pip install -r requirements_v1_1.txt / ...

  4. == 与 equals

    参考:http://www.cnblogs.com/dolphin0520/p/3592500.html

  5. Swift声明参考

    一条声明可以在你的程序里引入新的名字和构造.举例来说,你可以使用声明来引入函数和方法,变量和常量,或者来定义 新的命名好的枚举,结构,类和协议类型.你也可以使用一条声明来延长一个已经存在的命名好的类型 ...

  6. zepto源码--核心方法7(管理包装集)--学习笔记

    后面应该会有几篇连续介绍关于zepto包装集的文章.涉及的函数较多,就分别介绍,今天介绍几个使用关系获取包装集的方法.prev, next, first, last, siblings, eq pre ...

  7. 安卓仿微信Tab页用Fragment实现

    最终效果图如: 实现步骤: 新建项目tabdemo,我选的是4.0.3版本,然后依次新建三个Fragment,名字分别为:ChatFragment.FriendFragment.FindFragmen ...

  8. 怎样将BigDecimal转换成Int

    BigDecimal a=new BigDecimal(12.88); int b=a.intValue(); System.out.println(b);//b=12;

  9. 解决windows 10关机自动重启的问题

    自从windows 10推出来没多久,就给台式机安装了.可是,有点悲剧的是:每次关机,都会自动重启(restart). 之后也在网上找了一些解决方式,但还是没用.前天通过搜索”Windows 10 c ...

  10. RDIFramework.NET ━ 9.2 员工管理 ━ Web部分

    RDIFramework.NET ━ .NET快速信息化系统开发框架 9.2  员工管理 -Web部分 员工(职员)管理主要是对集团.企事业内部员工进行管理.在后面的章节可以看到有一个用户管理,这两者 ...