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 ...
随机推荐
- April 4 2017 Week 14 Tuesday
Problems are not stop signs, they are guidelines. 问题不是休止符,而是引向标. It is ture during our explorations ...
- Altium_Designer-怎么将“原理图的更改”更新到“pcb图”?
打开原理图,直击菜单栏>>Design,选择第一项,>>Update PCB Document...在弹出的对话框里面选择执行更改即可将原理图更新到工程下面对应的PCB.也可以 ...
- 什么是DTO?
DTO: Data Transfer Object Wikipedia定义:Data transfer object (DTO)[1][2] is an object that carries dat ...
- 在Nutz中如何配置多个数据库源,并且带事务控制
在Nutz中如何配置多个数据库源,并且带事务控制 发布于 560天前 作者 Longitude 995 次浏览 复制 上一个帖子 下一个帖子 标签: 无 在Nutz中如何配置多个数据库源, ...
- P1316 丢瓶盖
题目描述 陶陶是个贪玩的孩子,他在地上丢了A个瓶盖,为了简化问题,我们可以当作这A个瓶盖丢在一条直线上,现在他想从这些瓶盖里找出B个,使得距离最近的2个距离最大,他想知道,最大可以到多少呢? 输入输出 ...
- Python 二进制 八进制 十进制 十六进制
1.四种进制的表示方式 >>> 0b010 0b二进制 >>> 0x010 0x 十六进制 >>> 0o010 0o 八进制 >>&g ...
- Git配置和常用命令
Git配置 git config --global user.name "hunng" git config --global user.email "huangthin ...
- 第47章 QR-Decoder-OV5640二维码识别—零死角玩转STM32-F429系列
第47章 QR-Decoder-OV5640二维码识别 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.y ...
- wgan pytorch,pyvision, py-faster-rcnn等的安装使用
因为最近在读gan的相关工作,wgan的工作不得不赞.于是直接去跑了一下wgan的代码. 原作者的wgan是在lsun上测试的,而且是基于pytorch和pyvision的,于是要装,但是由于我们一直 ...
- 插入数据返回自增id及插入更新二合一
原文https://blog.csdn.net/dumzp13/article/details/50984413 JDBC: con.setAutoCommit(false); String sql ...