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 ...
随机推荐
- Web服务器——WSGI
1.什么是WSGI? WSGI全称 Web Server Gateway Interface,也可称作Python Web Server Gateway Interface,开始于2003年,为Pyt ...
- 2、数据类型和运算符——Java数据类型
一.强类型语言和弱类型语言: 1.1 强类型语言 强类型语言是一种强制类型定义的语言,一旦某一个变量被定义类型,如果不经过强制转换,则它永远就是该数据类型了,强类型语言包括Java..net .Pyt ...
- PCIeの数据链路层与物理层详解
数据链路层(DLL,Data Link Layer)的主要作用是进行链路管理(Link Management).TLP错误校验.Flow Control(流控制)和Link功耗管理.不仅可以接收发送来 ...
- Paper Reading_Distributed System
最近(以及预感接下来的一年)会读很多很多的paper......不如开个帖子记录一下读paper心得 Mark一个上海交通大学东岳网络工作室的paper notebook Mark一个大神的笔记 Ed ...
- 吴恩达机器学习7:代价函数(Cost function)
一.简介 1.在线性回归中,我们有一个这样的训练集,M代表训练样本的数量,假设函数即用来进行预测的函数是这样的线性函数的形式,我们接下来看看怎么选择这两个参数: 2.如下图中,怎么选择两个参数来更好的 ...
- NOTIFY - 生成一个通知
SYNOPSIS NOTIFY name DESCRIPTION 描述 NOTIFY 命令向当前数据库中所有执行过 LISTEN name, 正在监听特定通知条件的前端应用发送一个通知事件. 传递给前 ...
- GUI学习之二十二——QRubberBand学习总结
今天学习一种全新的输入控件——QRubberBand()控件(橡皮筋选中) 一.描述 QRubberBand()提供了一个矩形或西安来只是选择或边界的效果(就像在桌面上点击鼠标后拖拽拉出来的框一样), ...
- 常见 linux 命令
1.find find . //列出当前目录及子目录下的所有文件和文件夹 find /home -name "*.txt" //在/home目录下查找以.txt结尾的文件名或路径 ...
- zookeeper之二 zkCli客户端命令
ZooKeeper命令行界面(CLI)用于与ZooKeeper集合进行交互以进行开发.它有助于调试和解决不同的选项.要执行ZooKeeper CLI操作,首先打开ZooKeeper服务器(“bin/z ...
- noip2017简要题解。
重新写了一下去年的题来看看自己到底是有多傻逼. 小凯的疑惑 打表. 时间复杂度 搞了一大坨题面,但是真正有用的信息只有几个: 判断他给你的复杂度是多少. 判断当前循环进不进的去. 判断当前循环产生的贡 ...