Leetcode_160_Intersection of Two Linked Lists
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41593747
Intersection of Two Linked Lists
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.
思路:
(1)这道算法题是一道较老的题目。记得在数据结构书的习题中出现过。题目不难,但有几个点需要注意。
(2)需要注意:A:链表为空的判断;B:链表是否带有环的判断;C:两链表长度之差处的起始点的寻找。
(3)首先,对链表为空的判断,为空则返回null;
(4)其次,需设置两个临时变量A和B分别指向两链表的起始位置,分别遍历两链表并求得其长度,如果遍历完成A和B指向的不
是同一节点,则说明链表带有环,返回null;
(5)再次,比较两链表的大小,将较大的链表从其起始位置向后移动到两链表长度之差的位置,这样,两链表就出于同一起始位
置了;
(5)最后,判断起始位置对应节点是否相同,不相同则两节点分别后移,最后返回A或B即为结果。
算法代码实现如下所示(PS:题目要求时间复杂度为O(n),空间复杂度为O(1),大家有更好的算法希望能分享,谢谢):
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
int lenA = 0;
int lenB = 0;
ListNode A = headA;
ListNode B = headB;
if (A == null || B == null)
return null;
while (A.next != null) {
lenA++;
A = A.next;
}
while (B.next != null) {
lenB++;
B = B.next;
}
// 如果有环状的,终点会不一样,返回null
if (A != B) return null;
A = headA;
B = headB;
// 找到相差的个数,并移动到相同的地方开始比较
if (lenA > lenB) {
for (int i = 0; i < lenA - lenB; i++) {
A = A.next;
}
} else {
for (int i = 0; i < lenB - lenA; i++) {
B = B.next;
}
}
// 如果不相等则比较其后续是否相等
while (A != B) {
A = A.next;
B = B.next;
}
return A;
}
Leetcode_160_Intersection of Two Linked Lists的更多相关文章
- [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
题目简述: Write a program to find the node at which the intersection of two singly linked lists begins. ...
- [LintCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...
- 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 ...
- (LinkedList)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][003] Intersection of Two Linked Lists
[题目]: Write a program to find the node at which the intersection of two singly linked lists begins. ...
- LeetCode Intersection of Two Linked Lists
原题链接在这里:https://leetcode.com/problems/intersection-of-two-linked-lists/ 思路:1. 找到距离各自tail 相同距离的起始List ...
随机推荐
- 反射 类的加载 Schema DOM 解析方式和解析器 命名空间
Day15 反射 1.1 类的加载 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,连接,初始化三步来实现对这个类进行初始化. l 加载 就是指将class文件读入内存,并为之创建 ...
- 关于mysql安装到最后一步老是停留在starting server,显示无响应
从昨天晚上到今天安装MySQL花了好长的时间,一直是在后面starting server 这部就显示无响应,查资料了解到是MySQL有残留,有些注册表文件需要手动清理,下面是具体方法. 1.先用卸载软 ...
- 浅析JS异步执行机制
前言 JS异步执行机制具有非常重要的地位,尤其体现在回调函数和事件等方面.本文将针对JS异步执行机制进行一个简单的分析. 从一份代码讲起 下面是两个经典的JS定时执行函数,这两个函数的区别相信对JS有 ...
- 04_Struts2标签
1.通用标签: property标签: 用来输出值栈属性的值 如果value属性没有给出,ValueStack值栈栈顶对象的值被输出 许多情况下,EL表达式可以提供更简洁的语法 url标签: url方 ...
- MongoDB 自动增长
MongoDB 没有像 SQL 一样有自动增长的功能, MongoDB 的 _id 是系统自动生成的12字节唯一标识. 但在某些情况下,我们可能需要实现 ObjectId 自动增长功能. 由于 Mon ...
- Python3 SMTP发送邮件
SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式. python的smtplib提供了一 ...
- 整理的Java List Set Map是否有序,元素是否允许重复
整理的Java List Set Map是否有序,元素是否允许重复的说明,如下图:
- JBOSS EAP实战(2)-集群、NGINX集成、队列与安全
JBOSS HTTP的Thread Group概念 JBOSS是一个企业级的J2EE APP Container,因此它和任何一种成熟的企业级中间件一样具有Thread Group的概念.所谓Thre ...
- Ubuntu环境下Anaconda安装TensorFlow并配置Jupyter远程访问
本文主要讲解在Ubuntu系统中,如何在Anaconda下安装TensorFlow以及配置Jupyter Notebook远程访问的过程. 在官方文档中提到,TensorFlow的安装主要有以下五种形 ...
- 子库存-OU-库存组织-关系
SELECT hou.organization_id ou_org_id, --org_id hou.name ou_name, --ou名称 ood.organization_id org_org_ ...