[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 ...
随机推荐
- split分隔
split() : 把一个字符串分割成字符串数组 <script> var str="name=ww;value=ll"; var mm=str.split(" ...
- EDAS字体嵌入问题解决方法
提交IEEE EDAS文章时出现:“The paper PDF file cannot be accepted: Publishers require that PDF fonts are embed ...
- keil里面填数据
编译后寄存器和堆栈的内存数据可以直接写进去的. 寄存器,双击就可以,注意里面是十六进制 堆栈,也是十六进制,八位的 00 00 00 00 ,但这个是从右到左的,比如0x00000006 应该填 06 ...
- 跟着太白老师学python day11 函数名的应用 globals(), locals()
1. 函数名就是内存地址 def func(): ') print(func) >>>> <function func at 0x00000000003DC1E0> ...
- day08 跟着太白老师学python 文件操作
文件操作初识: 1. 文件路径 :d:/护士主妇空姐联系方式 (文件路径不要太过复杂,容易碰到转义字符的问题, 当碰到转义字符时,需要在前面+r,或者采用双斜杠(//)) 2. 编码方式 :utf- ...
- 使用NPM在项目中引入【lodash】
mkdir [文件名 ] 创建项目文件 mkdir lodashDemo cd [文件名] 进入项目文件 cd lodashDemo nvm -v 查看nvm版本,确定nvm已安装 nvm -v No ...
- 109. Convert Sorted List to Binary Search Tree (List; Divide-and-Conquer, dfs)
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- Mac shell使用技巧总结(转)
1.文件操作 常用目录 /Systme/Library/Extensions // 驱动所在目录 /User/XXX/Desktop // 桌面目录 资源库 chflags nohidden ~/Li ...
- 新手教程: 如何在新浪云计算SAE里部署代码
感谢 sou6 的投递 时间:2011-11-22 来源:老夏博客 SAE自2011-7-10日起,全面支持SVN代码部署,用户不仅可以通过任何SVN客户端部署代码,而且SAE现有的代码部署方式也已经 ...
- 85. Maximal Rectangle 由1拼出的最大矩形
[抄题]: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...