[LeetCode 题解]:Intersection of Two Linked Lists
前言
【LeetCode 题解】系列传送门: http://www.cnblogs.com/double-win/category/573499.html
1.题目描述
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
2. 题意
寻找两个链表的交点。
提示:
1. 如果两个链表没有交集,那么返回NULL。
2. 链表的结果不能发生变化。
3. 两个链表都没有环。
4. 请给出O(n)时间复杂度、O(1)空间复杂度的解法。
3. 思路
分别统计链表A和链表B的长度。
如果两个链表有交集,那么从交点CP开始,后续的所有节点都应该相同。如图所示C1->C2->C3.
两个链表不同的节点分别为a1,a2; b1,b2,b3;
比较两个链表的长度la,lb;
假设la>lb,那么链表A先遍历la-lb节点。从la-lb节点开始,两个链表的长度就相同了。然后依次比较各自节点是否相同,直到找到交点或者到达链表尾部。
4: 解法
class Solution {
public:
int getListLen(ListNode *head){
int len=0;
ListNode *root=head;
while(root){
root=root->next;
len++;
}
return len;
}
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
int alen=getListLen(headA),blen=getListLen(headB);
ListNode *la=headA,*lb=headB;
if(alen>blen){
while(alen!=blen){
la=la->next;
alen--;
}
}
if(alen<blen){
while(alen!=blen){
lb=lb->next;
blen--;
}
}
while(la!=lb){
la=la->next;
lb=lb->next;
}
if(!la||!lb){
return NULL;
}else{
return la;
}
}
};[LeetCode 题解]:Intersection of Two Linked Lists的更多相关文章
- [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 ...
- 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 求两个链表的交集
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. ...
- 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 ...
- 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 160. Intersection of Two Linked Lists 求两个链表的起始重复位置 --------- java
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. For ex ...
随机推荐
- 19_java之List和Set
01List接口的特点 A:List接口的特点: a:它是一个元素存取有序的集合. 例如,存元素的顺序是11.22.33.那么集合中,元素的存储就是按照11.22.33的顺序完成的). b:它是一 ...
- JVM常用参数设置
堆内存设置 示例: java -Xmx4550m -Xms4550m -Xss128k -XX:NewRatio=5 -XX:SurvivorRatio=5 -Xmx4550m:设置JVM最大可用内存 ...
- optparse模块
optparse模块主要是用来对参数的记录,相对来说比较灵活, 例子代码如下: #!/usr/bin/env python from optparse import OptionParser usag ...
- 通过Java代码装配Bean
上面梳理了通过注解来隐式的完成了组件的扫描和自动装配,下面来学习下如何通过显式的配置的装配bean 二.通过Java类装配bean 在前面定义了HelloWorldConfig类,并使用@Compon ...
- leetcode724
public class Solution { public int PivotIndex(int[] nums) { ) { ; } ; ]; if (left == right) { ; } ; ...
- **字符串格式化:%和.format
字符串格式化:%和.format .format在许多方面看起来更便利.对于%最烦人的是它无法同时传递一个变量和元组.你可能会想下面的代码不会有什么问题: "hi there %s" ...
- 阻塞IO,非阻塞IO,异步IO和非异步IO 的区别
最近在研究java IO.NIO.NIO2(或者称AIO)相关的东西,有些概念还是要明确下. 按照<Unix网络编程>的划分,IO模型可以分为:阻塞IO.非阻塞IO.IO复用.信号驱动IO ...
- 3.Hadoop集群搭建之Zookeeper安装
前期准备 下载Zookeeper 3.4.5 若无特殊说明,则以下操作均在master节点上进行 1. 解压Zookeeper #直接解压Zookeeper压缩包 tar -zxvf zookeepe ...
- 封装baseservice
package com.huawei.base; import java.io.Serializable;import java.util.List; public abstract class Ba ...
- linux: 空指令(:)
:指令 描述: 空命令,除了参数替换和重定向外不执行任何操作,总是保证退出码为0. eg1:创建文件(不需要调用其它程序,速度更快) :>/path/to/file 测试: 创建10000个不存 ...