mycode 用了反转链表,所以不符合题意

参考:

思路:

1 先让长的链表先走,然后相同长度下看是否相遇

class Solution(object):
def getIntersectionNode(self, headA, headB):
"""
:type head1, head1: ListNode
:rtype: ListNode
"""
if not headA or not headB:
return None
def cal(head):
count = 0
while head:
count += 1
head = head.next
return count
countA = cal(headA)
countB = cal(headB)
plus = countA - countB
if plus > 0:
while plus:
headA = headA.next
plus -= 1
left = countB
else:
plus = abs(plus)
while plus:
headB = headB.next
plus -= 1
left = countA
while left: #这里无论是headA还是headB都可以啦,因为两个人步伐已经一致啦
if headA == headB:
return headA
headA = headA.next
headB = headB.next
left -= 1
return None

2  让短的链表走到头后,再从长链表的头走起,这样当长链表走完后,短链表刚好在长链表上走了长度的差值的步数,所以长链表再从短链表头开始走的时候,相当于两个人起跑线相同啦

class Solution(object):
    def getIntersectionNode(self, headA, headB):         if not headA or not headB:
            return None
   
        p,q = headA , headB            while p != q: # 当p不等于q时执行下面程序
            p = headB if p is None else p1.next # 如果p不是none,就取下一个值,是NONE就让p = headB
            q = headA if q is None else q.next # 如果q不是none,就取下一个值,是NONE就让q = headA
            
        return p1 # p ,q相等有两种情况,一种是相交了,输出相交点,一种是不相交,输出了NONE

leetcode-mid-Linked list-160 Intersection of Two Linked Lists-NO的更多相关文章

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

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

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

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

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

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

  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 解题报告(Python)

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

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

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

  10. ✡ leetcode 160. Intersection of Two Linked Lists 求两个链表的起始重复位置 --------- java

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

随机推荐

  1. [2019沈阳网络赛D题]Dawn-K's water(点分治)

    题目链接 题意为求出树上任意点对的距离对3取余的和. 比赛上听到题意就知道是点分治了,但是越写越不对劲,交之前就觉得会T,果不其然T了.修修改改结果队友写了发dp直接就过了Orz. 赛后想了想维护的东 ...

  2. 02: CI(持续集成)/CD(持续交付/持续部署)

    1.1 持续集成.持续交付 介绍   参考博客:https://www.cnblogs.com/cay83/p/8856231.html 1.传统交付 1. 传统软件的开发与交付的周期都很漫长,从需求 ...

  3. jQuery之链式编程

    使用的思想:隐式迭代. <button>快速</button> <button>快速</button> <button>快速</but ...

  4. 在Python中使用protobuf2.6.1 string format utf-8 and unicode error

    版本信息: protobuf: v2.6.1 python: 2.7 关于在Python中使用protobuf时 string格式字段的编码问题 在python中编码格式多采用utf-8格式.而pro ...

  5. k3 cloud付款单提示余额不足,科目余额表中余额为正,银行存款流水账中未负数

    对比科目余额表中的科目明细账和银行存款流水账,发现科目余额表不全,这说明有部分凭证没做,付款的时候验证的主要以银行流水账为主(主要来自现金流量表),这时候需要调整出纳部分和总账部分

  6. (架构)React Native 导出项目全局共用组件的模块

    自定义组件全局使用(类似如下) import { ReactNavComponent, Widget, Util } from 'rn-yunxi'; const { RegexpUtil, Stor ...

  7. 企业网站的SSL签证生产测试以及https配置方法

    这一次要做企业网站怎么获得安全的数字证书,没有数字证书的话,在浏览器访问网站的时候会跳出不安全界面,而且钓鱼网站也会让用户进去个假网站,一般企业可以去阿里云去买数字证书,买好之后浏览器便会加载这个数字 ...

  8. [易学易懂系列|golang语言|零基础|快速入门|(二)]

    现在我们来写代码,首先我们要新建一个项目. 新建项目: 点击:File>>New>>Project...如下图: 在New Project窗口,Location:输入:“goP ...

  9. ASP.NET Core 基础知识(五) 依赖关系注入

    参考https://www.cnblogs.com/jesse2013/p/di-in-aspnetcore.html 和 https://www.cnblogs.com/artech/p/net-c ...

  10. C#的Class的几个修饰符

    none or internal     类只能在当前的工程中访问 Public                    类可以在任何地方访问 abstract or internal abstract ...