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 ...
随机推荐
- 【js基础修炼之路】— null和undefined的区别
在近期的复习期间遇到null和nudefined,于是通过查找资料,想写一篇文章来说明他们的区别.. javaScript高级程序设计: 在使用var声明变量但未对其加以初始化时,这个变量的值就是un ...
- MySQL入门很简单: 14MySQL日志
二进制日志: 以二进制文件的形式记录了数据库中的操作,但不记录查询语句 错误日志: 记录MySQL服务器的启动,关闭和运行错误等信息 通用查询日志: 记录用户登录和记录查询的信息 慢查询日志: 记录执 ...
- C4C和CRM里获取当前登录用户分配的Organization Unit信息
C4C 如何查看某个用户分配的组织单元ID: 在Employee的Organization Data区域内看到分配的组织名称,如下图红色下划线所示: 现在的需求就是使用ABSL获取当前登录用户分配的O ...
- Leetcode 22. Generate Parentheses Restore IP Addresses (*) 131. Palindrome Partitioning
backtracking and invariant during generating the parathese righjt > left (open bracket and cloas ...
- IOS ScrollView的使用 and delegate
ScrollView常用的属性设置 //设置内容尺寸 // CGFloat contentH=self.lastBtn.frame // .origin.y+self.lastBtn.frame.si ...
- 使用selenium grid遇到的坑,解决不了冲突,只有避免
背景:使用到grid做分发,已经有两周,运行较稳定,分发也健壮,不知道是不是要因为运行量小,服务器也没出问题,稳定到两周后,发现分发到A服务器(10.40.2.113)和B服务器(10.40.2.11 ...
- bootstrap中模态框、模态框的属性
工作中有需要用到模态框的可以看看 <div class="modal fade" id="userModal" tabindex="-1&quo ...
- CUDA线性内存分配
原文链接 概述:线性存储器可以通过cudaMalloc().cudaMallocPitch()和cudaMalloc3D()分配 1.1D线性内存分配 1 cudaMalloc(void**,int) ...
- C#面向对象的基本概念
“面向对象=对象+类+继承+通信”.如果一个软件系统使用了这样四个概念进行设计和实现,我们就可以认为这个软件系统是面向对象的. 一.一切都是对象 1. 对象概述 对象可以表示几乎所有的实物和概念.比如 ...
- scp 远程拷贝
scp拷贝时,可以从本地拷贝到远程,也可以远程拷贝到本地.唉,我记得之前是都用过的,但是现在完全忘了,今天同事写出来我才意识到自己之前用过的. 唉,这几年在新单位如果不好好积累一下理论上的东西,真的是 ...