leetcode 【 Intersection of Two Linked Lists 】python 实现
题目:
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.
代码:oj在线测试通过 Runtime: 1604 ms
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @param two ListNodes
# @return the intersected ListNode
def getListLen(self,head):
length = 0
while head is not None:
length += 1
head = head.next
return length def getIntersectionNode(self, headA, headB):
if headA is None or headB is None:
return None hA = headA
hB = headB lenA = self.getListLen(hA)
lenB = self.getListLen(hB) if lenA > lenB :
distance = lenA - lenB
for i in range(0,distance):
hA = hA.next
if lenA < lenB :
distance = lenB -lenA
for i in range(0,distance):
hB = hB.next intersection = None
while hA is not None and hB is not None:
if hA == hB:
return hA
else:
hA = hA.next
hB = hB.next
return intersection
思路:
1. 首先要记录两个Linked List的长度
2. 双指针 分别指向两个List 指向长List的先走若干步,获得一个新的Linked List表头
3. 逐一比较两个List的每个指针的值是否相等:直到找到相等的,或者到None
leetcode 【 Intersection of Two Linked Lists 】python 实现的更多相关文章
- 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] 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
原题链接在这里:https://leetcode.com/problems/intersection-of-two-linked-lists/ 思路:1. 找到距离各自tail 相同距离的起始List ...
- LeetCode——Intersection of Two Linked Lists
Description: Write a program to find the node at which the intersection of two singly linked lists b ...
- [LeetCode] 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 (找交叉点)
题意: 给两个链表,他们的后部分可能有公共点.请返回第一个公共节点的地址?若不重叠就返回null. 思路: 用时间O(n)和空间O(1)的做法.此题数据弱有些弱. 方法(1)假设两个链表A和B,用两个 ...
- [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 ...
- [LintCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...
- 2016.5.24——Intersection of Two Linked Lists
Intersection of Two Linked Lists 本题收获: 1.链表的输入输出 2.交叉链表:这个链表可以有交叉点,只要前一个节点的的->next相同即可. 题目:Inters ...
随机推荐
- Selenium入门20 等待时间
自动化过程中有的页面元素加载慢或者需要等待特定条件执行后续步骤,此时需添加等待时间: 1 time.sleep() 固定等待时间,需import time 2 webdriver隐式等待 无需引入包 ...
- POST信息模拟登录获取页面内容
最近项目里有一个是要模拟登录后,访问固定页面获取内容的要求,一开始用JQ AJAX好像不支持跨域请求.后使用.net中HttpWebRequest对象来获取.一开始访问总是无法在第二个页面正常访问,好 ...
- 使用browsermob代理出现错误java.lang.NoClassDefFoundError: org/littleshoot/proxy/HttpFiltersSource
使用browsermob代理做埋点数据,maven配置的包如下 <dependency> <groupId>net.lightbody.bmp</groupId> ...
- AJAX(二):HTTP头部信息
每个http请求和响应都会带有相应都头部信息,其中有的对开发人员有用,有的页没有什么用默认情况下,发送xhr请求的同时,还有发送下列头部信息 Accept:浏览器能够处理的内容类型 Accept-Ch ...
- git常用命令(三)
====================================================================== 本地仓库操作 ====================== ...
- 洛谷P1437 [HNOI2004]敲砖块(dp)
题目背景 无 题目描述 在一个凹槽中放置了 n 层砖块.最上面的一层有n 块砖,从上到下每层依次减少一块砖.每块砖 都有一个分值,敲掉这块砖就能得到相应的分值,如下图所示. 14 15 4 3 23 ...
- Python——字典
字典是一种key-value 的 数据类型,使用就想我们上学用的字典.可以通过笔画,字母来查对应页的详细内容. 特性:1. 字典是无须的.(如果光打印字典里的字符串,那么排序不会按照顺序排,因为字典是 ...
- Logrotate实现Catalina.out日志每俩小时切割
一.Logrotate工具介绍 Logrotate是一个日志文件管理工具,它是Linux默认自带的一个日志切割工具.用来把旧文件轮转.压缩.删除,并且创建新的日志文件.我们可以根据日志文件的大小.天数 ...
- TCP/IP协议之http和https协议
一.TCP/IP协议 TCP/IP 是不同的通信协议的大集合. 1.TCP - 传输控制协议 TCP 用于从应用程序到网络的数据传输控制. TCP 负责在数据传送之前将它们分割为 IP 包,然后在它们 ...
- PHPExcel 中文使用手册详解
/** * * execl数据导出 * 应用场景:订单导出 * @param string $title 模型名(如Member),用于导出生成文件名的前缀 * @param array $cellN ...