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 ...
随机推荐
- [BZOJ4569] [Luogu 3295] [SCOI2016]萌萌哒(并查集+倍增)
[BZOJ4569] [Luogu 3295] [SCOI2016]萌萌哒(并查集+倍增) 题面 有一个n位的十进制数a(无前导0),给出m条限制,每条限制\((l_1,r_1,l_2,r_2)(保证 ...
- pistat 查看进程状态
该pidstat命令用于监视当前正在由Linux内核管理的各个任务.对于使用选项-p选择的每个任务,或者如果使用了选项-p ALL,则它将写入Linux内核管理的 每个任务的标准输出活动.不选择任何任 ...
- CSP-S全国模拟赛第二场 【nan】
A.count 本场比赛最难的题... 隔板法组合数容斥 xjb 搞搞就好了 //by Judge #include<cstdio> #include<iostream> #d ...
- C#修改电脑桌面图
win32helper public class Win32Helper { [DllImport("user32.dll", EntryPoint = "SystemP ...
- Python 自定义三方库
一.注册一个pypi账号 https://pypi.org/ 二.github上创建一个项目 https://github.com/ 三.编写自己的python项目 项目结构(参考):https:// ...
- Apache 网站日志分析
1.获得访问前 10 位的 ip 地址 [root@apache ~]# cat access_log |awk '{print $1}'|sort|uniq -c|sort -nr|head -10 ...
- Educational Codeforces Round 32 Almost Identity Permutations CodeForces - 888D (组合数学)
A permutation p of size n is an array such that every integer from 1 to n occurs exactly once in thi ...
- [HTTP知识体系]前端常用的一些参数
1.http常见状态码(status code) 200(成功) 服务器已成功处理了请求.通常,这表示服务器提供了请求的网页. 301 (永久移动) 请求的网页已永久移动到新位置. 服务器返回此响应( ...
- pandas 的axis参数的理解
# pandas的axis参数怎样理解? # axis=0 或者 "index": # 如果是单行操作,就指的是某一行 # 如果是聚合操作,指的是跨行cross rows # ax ...
- vue2.0 之 生命周期
一.vue1.x与vue2.x生命周期的变化区别及含义表(图表摘自网络) 二.vue2.x生命周期图和各阶段具体含义 beforecreated:el 和 data 并未初始化 created: ...