【LeetCode】160. Intersection of Two Linked Lists 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/intersection-of-two-linked-lists/description/
题目描述
Write a program to find the node at which the intersection of two singly linked lists begins.
For example, the following two linked lists:
A: a1 → a2
c1 → c2 → c3
B: b1 → b2 → b3
begin to intersect at node c1.
Notes:
- If the two linked lists have no intersection at all, return null.
- The linked lists must retain their original structure after the function returns.
- You may assume there are no cycles anywhere in the entire linked structure.
- Your code should preferably run in O(n) time and use only O(1) memory.
Credits: - Special thanks to @stellari for adding this problem and creating all test cases.
题目大意
找出两个链表的最早公共元素。
解题方法
双指针
第一次遍历时,如果两者的非公共元素的个数正好相等,那么一定能找到相同元素;如果非公共元素个数不等,那么在一次遍历之后,两者的指针的差距就是非公共元素的个数差。这样翻转之后,指针的差距正好弥补了非公共元素的差,这样,第二次遍历要么一定相遇,要么两者没有公共元素,返回None。
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def getIntersectionNode(self, headA, headB):
"""
:type head1, head1: ListNode
:rtype: ListNode
"""
if headA is None or headB is None:
return None
pA = headA
pB = headB
while pA is not pB:
pA = headB if pA is None else pA.next
pB = headA if pB is None else pB.next
return pA
二刷的时候,感觉写的解法更为容易理解。
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def getIntersectionNode(self, headA, headB):
"""
:type head1, head1: ListNode
:rtype: ListNode
"""
len1, len2 = 0, 0
moveA, moveB = headA, headB
while moveA:
len1 += 1
moveA = moveA.next
while moveB:
len2 += 1
moveB = moveB.next
if len1 < len2:
for _ in range(len2 - len1):
headB = headB.next
else:
for _ in range(len1 - len2):
headA = headA.next
while headA and headB and headA != headB:
headA = headA.next
headB = headB.next
return headA
栈
因为后面的元素是相等的,所以使用栈把相等元素都弹出来,那么不等元素就是所求。
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def getIntersectionNode(self, headA, headB):
"""
:type head1, head1: ListNode
:rtype: ListNode
"""
stack1, stack2 = [], []
while headA:
stack1.append(headA)
headA = headA.next
while headB:
stack2.append(headB)
headB = headB.next
pre = None
while stack1 and stack2:
s1 = stack1.pop()
s2 = stack2.pop()
if s1 != s2:
return pre
else:
pre = s1
return pre
日期
2017 年 8 月 27 日
2018 年 11 月 26 日 —— 11月最后一周!
【LeetCode】160. Intersection of Two Linked Lists 解题报告(Python)的更多相关文章
- [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: Intersection of Two Linked Lists 解题报告
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(2个链表的公共节点)
Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...
- 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 ...
- Java [Leetcode 160]Intersection of Two Linked Lists
题目描述: Write a program to find the node at which the intersection of two singly linked lists begins. ...
- 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
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 ...
随机推荐
- Requests的安装和使用
一.Requests的安装1.pip3 install requests2.验证 import requests 不报错即可
- 业务逻辑审批流、审批流、业务、逻辑、面向对象、工作方式【c#】
------需求分析:--------1.先按照实际线下流程说这是什么事情,实际要干什么.2.再转换为面向对象-页面的操作流程,演示demo3.再与相关人员沟通是否可行需要什么地方修正.4.最终:线上 ...
- C语言中的字符和整数之间的转换
首先对照ascal表,查找字符和整数之间的规律: ascall 控制字符 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 56 8 ...
- Yarn 生产环境核心配置参数
目录 Yarn 生产环境核心配置参数 ResourceManager NodeManager Container Yarn 生产环境核心配置参数 ResourceManager 配置调度器 yarn. ...
- 学习Java 2021/10/7
java重写Override 重载Overload 重写是子类对父类的允许访问的方法的实现过程进行重新编写,返回值和形参都不能改变.即外壳不变,核心重写 重写规则: 参数列表与被重写方法的参数列表必须 ...
- 同步阻塞IO模型
同步阻塞IO模型 有上篇IO模型中的,同步阻塞IO模型,我们能够知道,用户线程发起请求后就一直阻塞的等待 内核完成准备数据.数据拷贝的工作.并且返回成功的指示. 实现 使用java来实现同步阻塞IO模 ...
- Spark(九)【RDD的分区和自定义Partitioner】
目录 spark的分区 一. Hash分区 二. Ranger分区 三. 自定义Partitioner 案例 spark的分区 Spark目前支持Hash分区和Range分区,用户也可以自定义分区 ...
- Oracle中分割逗号函数REGEXP_SUBSTR
最近优化FORM中的查询条件遇到某个字段可以选取多个值的问题,思路当然就是选取时将多个值通过某个符号拼接起来,查询数据的时候将拼接后的字符串按照符号分割开,在分割逗号的时候用到了一个新的方法REGEX ...
- ubuntu qq/微信
Ubuntu qq&微信安装/启动脚本. Docker 本脚本依赖Docker,需要提前安装好Docker环境.参考https://yeasy.gitbooks.io/docker_pract ...
- 阿里巴巴Java开发手册摘要(一)
一命名风格 1.代码中的命名均不能以下划线或美元符号开始,也不能以下划线或美元符号结尾. 反例:_name / $name / name_ / name$ 2.类名使用UpperCamelCase风格 ...