leetcode-mid-Linked list-160 Intersection of Two Linked Lists-NO
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的更多相关文章
- 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 ...
- [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 ...
- [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 ...
- 【一天一道LeetCode】#160. Intersection of Two Linked Lists
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
- 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 ...
- [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 ...
- 【LeetCode】160. Intersection of Two Linked Lists 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 栈 日期 题目地址:https://leet ...
- 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 ...
- 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 ...
- ✡ 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 ...
随机推荐
- uboot 主Makefile分析
一. Makefile 配置 1.1. make xxx_config 1.1.1. 笔者实验时是make x210_sd_config a. x210_sd_config是Makefile下的一个目 ...
- Delphi主消息循环研究(Application.Run和Application.Initialize执行后的情况)
Application.Initialize; Application.CreateForm(TForm1, Form1); Application.Run; 第一步,貌似什么都不做,但如果提前定义I ...
- 连连看(简单搜索)bfs
连连看Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- myeclipse2014下卸载,安装maven插件。
转自:https://blog.csdn.net/gaoshang10/article/details/21177893 一.卸载方法: 点击Help->About Myeclipse Ente ...
- PHP7.2 、git、swoole安装
一.安装php 1.安装gcc yum -y install gcc gcc-c++ 2.安装一些库 yum -y install php-mcrypt libmcrypt-devel libxml2 ...
- Java设计模式之——代理设计模式
1.什么是代理设计模式 所谓代理模式是指客户端并不直接调用实际的对象,而是通过调用代理,来间接的调用实际的对象. 这里举一个栗子:就拿我们平时租房子来举例子,好比租客和房主之间的关系,我们租房子往往不 ...
- Linux系统性能测试工具(六)——磁盘io性能工具之dd
本文介绍关于Linux系统(适用于centos/ubuntu等)的磁盘io性能测试工具-dd.磁盘io性能测试工具包括: fio: dd
- tf模型可视化工具
一方面可以用tensorboard来可视化,更方便的是用如下网址: https://lutzroeder.github.io/netron/
- vim 修改复制过来的代码缩进
命令模式下 :1,9< //1至9行回退一个tab :1,9> //1至9行缩进一个tab 让不可打印字符心事出来::set list
- java 类记载器
转载: https://blog.csdn.net/javazejian/article/details/73413292