#-*- 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. subprocess模块还提供了很多方便的方法来使得执行 shell 命令

    现在你可以看到它正常地处理了转义. 注意 实际上你也可以在shell=False那里直接使用一个单独的字符串作为参数, 但是它必须是命令程序本身,这种做法和在一个列表中定义一个args没什么区别.而如 ...

  2. git pull all braches

    控制台下执行如下: git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote ...

  3. Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

    /** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode ri ...

  4. iOS 宏(define)与常量(const)的正确使用

    在iOS开发中,经常用到宏定义,或用const修饰一些数据类型,经常有开发者不知怎么正确使用,导致项目中乱用宏与const修饰 你能区分下面的吗?知道什么时候用吗? #define HSCoder @ ...

  5. docker interact example

    此为docker 第一篇,插下杂七杂八的东西,好找,就这么简单,,,, yum -y install docker-io //install cp /var/tmp/cap.data /var/lib ...

  6. 关于thenao.scan() fn函数参数的说明

    theano.scan()原型: theano.scan( fn, sequences=None, outputs_info=None, non_sequences=None, n_steps=Non ...

  7. Struts(八):动态方法调用

    动态方法调用:通过url动态调用action中的方法. 默认情况下,Struts的动态方法调用处于禁用状态. 测试定义一个action类: package com.dx.actions; public ...

  8. Leetcode: Minimum Genetic Mutation

    A gene string can be represented by an 8-character long string, with choices from "A", &qu ...

  9. iftop命令命令详解

    iftop命令命令详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在Linux命令中有很多内置命令,和外置命令,但是内部命令的功能毕竟是有限的,比如ifconfig,它就不能看 ...

  10. Winform 主窗体更换 构造函数传值

    制作登录窗体: 制作一个登陆窗体,实现点击按钮关闭此窗体并打开另一个窗体 直接在按钮点击事件中,实例化一个想要打开的窗体 使用show方法打开,并把登陆窗体的visible属性改为false Form ...