#-*- 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. 20145334 第五次 java 实验报告

    实验内容 1.掌握Socket程序的编写: 2.掌握密码技术的使用: 3.设计安全传输系统. 我和20145306张文锦组队编程 http://www.cnblogs.com/besti145306/ ...

  2. xml、文件操作功能类

    我一个项目中用到的,里面的方法不是太通用,但是可以从里面找到一些有用的代码,以后慢慢添补更新: FileUtil.xml package com.novel.util; import java.io. ...

  3. Linux学习笔记---用户管理---帐号管理

    root管理 (1)新增用户:useradd -u 指定UID -g 指定GID -G 作为组员添加到某个组 -M 不创建主用户目录 -m 创建主用户目录 -c 用户信息说明列 -d 指定某个目录为主 ...

  4. Read4096

    Given API: int Read4096(char* buf); It reads data from a file and records the position so that the n ...

  5. PHP获取远程网站的服务器时间

    <?php function get_time($server){ $data  = "HEAD / HTTP/1.1\r\n"; $data .= "Host:  ...

  6. python中多进程(multiprocessing)

    一.multiprocessing中使用子进程概念 from multiprocessing import Process 可以通过Process来构造一个子进程 p = Process(target ...

  7. Ignoring Extra Elements in mongoDB C# Driver

    MongoDB删除字段后会报错: Element ... does not match any field or property of class Customer. 需要在实体类增加 [BsonI ...

  8. CentOS7配置日志(VirtualBox)

    版本为CentOS-Minimal 1.VirtualBox下安装CentOS. 新建虚拟机 下载CentOS,放入盘片,启动虚拟机,按提示开始安装(建议内存1G,硬盘10G以上)   2. 设置网络 ...

  9. LeetCode Read N Characters Given Read4

    原题链接在这里:https://leetcode.com/problems/read-n-characters-given-read4/ 题目: The API: int read4(char *bu ...

  10. soap和wsdl的定义

    SOAP(Simple Object Access Protocol)是一个严格定义的信息交换协议,用于在Web Service中把远程调用和返回封装成机器可读的格式化数据.事实上SOAP数据使用XM ...