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 ...
随机推荐
- python--ftp服务器(pyftpdlib)
# -*- coding: utf-8 -*-# @Time : 2018/4/11 16:47# @Author : liuxiaobing# @File : test2.py# @Software ...
- textarea不能使用maxlength
知道文本框有个maxlength属性,有次开发项目中使用了textarea标签,没去看文档,直接加了maxlength属性,且有效果没有报错,喜滋滋的用了,结果没两天就测试出了bug 问题描述:文本域 ...
- linux系统运维面试题简答
1. 简述常用高可用技术 解答: Keepalived:Keepalived是一个保证集群高可用的服务软件,用来防止单点故障,使用VRRP协议实现.在master和backup之间通过mast ...
- MFC回车事件
这是一个使用MFC开发关于设备控制的windows应用程序 通过该项目我学到的内容: 继承的好处 应用程序的界面是与应用程序的代码有一定的对应关系的,界面中不同的控件对应不同的类,首先就是需要一个对话 ...
- jQuery 遍历 – 同胞(siblings)
同胞拥有相同的父元素. 通过 jQuery,您能够在 DOM 树中遍历元素的同胞元素. 在 DOM 树中水平遍历 有许多有用的方法让我们在 DOM 树进行水平遍历: siblings() next() ...
- Spark核心类:SQLContext和DataFrame
http://blog.csdn.net/pipisorry/article/details/53320669 pyspark.sql.SQLContext Main entry point for ...
- Activity平移动画
Activity平移动画 效果图 添加动画文件 在res下添加anim文件夹,在anim下添加几个动画文件,分别是进入和退出的动画时间和移动距离,属性很简单,一看就懂,不磨叽了. tran_next_ ...
- JobTracker,TaskTracker简述
JobTracker 负责接收用户提交的作业,负责启动.跟踪任务执行.JobSubmissionProtocol是JobClient与JobTracker通信的接口.InterTrackerProto ...
- x264源代码简单分析:概述
===================================================== H.264源代码分析文章列表: [编码 - x264] x264源代码简单分析:概述 x26 ...
- Java学习之参数传递详解
Java中的参数传递问题: 基本类型:形式参数的改变对实际参数没有影响.在参数传递过程中,形参和实参占用了两个完全不同的内存空间. 引用类型:形式参数的改变直接影响实际参数.在参数传递的过程中,形参和 ...