#-*- 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. loading.gif

  2. LNMP-Linux下Nginx+MySQL+PHP+phpMyAdmin+eAcelerator一键安装包

    LNMP一键安装包是一个用Linux Shell编写的可以为CentOS/RadHat.Debian/Ubuntu VPS(VDS)或独立主机安装LNMP(Nginx.MySQL.PHP.phpMyA ...

  3. c# word 转pdf 导出失败,因为此功能尚未安装

    savePDF应该是office2007以上的版本才支持的,而且必须是完整版的office. 如果2007提示这个错误,还需要安装一个插件 http://download.microsoft.com/ ...

  4. ORA-01436: 用户数据中的CONNECT BY 循环

    起始地     目的地     距离(公里)A             B             1000A             C             1100A             ...

  5. linux查看是否被入侵

    一.检查系统日志 lastb命令检查系统错误登陆日志,统计IP重试次数 二.检查系统用户 1.cat /etc/passwd查看是否有异常的系统用户 2.grep “0” /etc/passwd查看是 ...

  6. Android Studio 开发环境设置

    修改字体(File=>Settings 按下如所图设计字体) 显示行号.空格 用 制表符 代替 空格 显示 最终代码显示如下图 格式化代码 使用 Ctrl+Alt+L

  7. Performance Analyzer Tool

    PAL工具的使用大同小异,网上看到这篇文章挺不错的,直接翻译过来.如果你在过去有Exchange性能问题,你肯定知道有很多可变因素会影响Exchange整体性能,有时需要很长的时间才能找到问题的根源, ...

  8. mvc 数据验证金钱格式decimal格式验证

    mvc 数据验证金钱格式decimal格式验证 首先看下代码 /// <summary> /// 产品单价 /// </summary> [Display(Name = &qu ...

  9. JS中对this的理解

    // this: 指的是调用 当前 方法(函数)的那个对象 <script> function fn1(){ this } 情况1:fn1()                  //这里t ...

  10. CsvHelper

    写66666行两个数字 用CsvHelper里的ExcelSerializer 400ms SaveAs 200ms 共600ms 直接用StreamWriter 150ms 行数可以超过65536 ...