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 ...
随机推荐
- adb 配置连接
一. adb环境安装 1.1. windown 驱动安装 1. 下载驱动(ADB Kits):http://adbshell.com/downloads 2. adb 测试 <1>. 解压 ...
- 洛谷 P2196 挖地雷 & [NOIP1996提高组](搜索,记录路径)
传送门 解题思路 就是暴力!!! 没什么好说的,总之,就是枚举每一个起点,然后暴力算一遍以这个点为起点的所有路径,在算的过程中,只要比目前找到的答案更优,就有可能是最后的答案,于是就把路径更新一遍,保 ...
- python是强语言还是弱语言?
python是强语言还是弱语言,没有一个具体官方的说法 数据类型也就是变量类型,一般编程语言的变量类型可以分成下面两类. 静态类型与动态类型 静态类型语言:一种在编译期间就确定数据类型的语言.大多数静 ...
- 常用的 Python 标准库都有哪些?
标准库:os 操作系统,time 时间,random 随机,pymysql 连接数据库,threading 线程,multiprocessing进程,queue 队列. 第三方库:django 和 f ...
- ps的一点快捷键
选区工具快捷键(shift alt很重要) 按M键切换到选区工具 矩形选区-> shift 正方形 shift+m 矩形/椭圆来回切换 参考线:alt+v 选中上方的工具 alt+v+e 新建参 ...
- scala学习笔记(9)
一.高阶函数 --------------------------------------------------- 1.作为值的函数:在Scala中,函数是头等公民,就和数字一样. import s ...
- 轮播图--js课程
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Python numpy数据的保存和读取
在科学计算的过程中,往往需要保存一些数据,也经常需要把保存的这些数据加载到程序中,在 Matlab 中我们可以用 save 和 lood 函数很方便的实现.类似的在 Python 中,我们可以用 nu ...
- mysql常见函数及其用例
函数调用:select 函数名(实参列表) [from 表]; 函数分类: 1.单行函数 如 concat.length.ifnull等. 2.分组函数 功能:做统计使用,又称为统计函数.聚合函数.组 ...
- 64bit机器编译32bit汇编
sudo apt-get install gcc-multilib sudo apt-get install g++-multilib gcc -m32 -S a.c -o a.s gcc -m64 ...