[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 ...
随机推荐
- 24_java之转换流和缓冲流
01转换流概述 * A: 转换流概述 * a: 转换流概述 * OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的字符编码表,将要写入流中的字符编码成字节 * 将字符串按照指 ...
- interrupt 1 using 1
释疑:void Timer0() interrupt 1 using 1 Timer0 是函数名,随便取的 interrupt xx using y 跟在interrupt 后面的 ...
- Eclipse注释配置
新的文件/** * @ClassName: ${type_name} * @Description: ${todo} * @author ${user} * @date ${date} ${time ...
- ui-router 留存
学习 ui-router - 路由控制 022014年01月 参考原文:https://github.com/angular-ui/ui-router/wiki/URL-Routing 在你的应用中大 ...
- JS中如何获取当前时间及让时间格式化
JS中获取当前时间和JAVA里获取当前时间一样,都是直接new Date即可.不同的是,JS中用var date=new Date();JAVA中用Data data=new Date();注:JS中 ...
- java反射(Field的应用)
//$Id: DirectPropertyAccessor.java 11405 2007-04-15 12:50:34Z max.andersen@jboss.com $ package org.h ...
- sqlserver,oracle,mysql等的driver驱动,url怎么写
oracle driver="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@localhost:1521 ...
- web前端整套面试题(二)--今日头条面试题
12道单选,7道不定项选择,2道编程题 一.单选(12题) 1.[单选题]在HTML中,( )可以在网页上通过链接直接打开邮件客户端发送邮件. A.<a href=”telnet:ming.zh ...
- Android 多分辨率多屏幕适配
请参见文章:http://blog.csdn.net/jiangxinyu/article/details/8598046 文章描述非常清晰.
- Phong Shading
[Phong Shading] The most serious problem with Gouraud shading occurs when specular highlights are fo ...