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 example, the following two linked lists:
begin to intersect at node c1.
Notes:
If the two linked lists have no intersection at all, return null.
The linked lists must retain their original structure after the function returns.
You may assume there are no cycles anywhere in the entire linked structure.
Your code should preferably run in O(n) time and use only O(1) memory.
分析
给定两个链表,求它们的交叉节点。
要求,时间复杂度在O(n)内,空间复杂度为O(1)
两个链表的长度不定,但是交叉节点的后续节点全部相同,所以先求得每个链表的长度lenA和lenB,将较长的链表先移动|lenA−lenB|个位置,然后同时后移,遇到的第一个值相等的节点既是要求的交叉节点。
AC代码
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if (!headA || !headB)
return NULL;
ListNode *p = headA, *q = headB;
//求出输入两个链表的长度
int lenA = 0, lenB = 0;
while (p)
{
++lenA;
p = p->next;
}//while
while (q)
{
++lenB;
q = q->next;
}//while
//让长的链表先移动多出的节点
p = headA;
q = headB;
if (lenA > lenB)
{
int i = 0;
while (p && i < lenA - lenB)
{
p = p->next;
++i;
}//while
}
else{
int j = 0;
while (q && j < lenB - lenA)
{
q = q->next;
++j;
}//while
}
while (p && q && p->val != q->val)
{
p = p->next;
q = q->next;
}//while
return p;
}
};
LeetCode (160) 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 ...
- (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 ...
- 【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 whic ...
- [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: Intersection of Two Linked Lists 解题报告
Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...
- 160. Intersection of Two Linked Lists【easy】
160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
随机推荐
- 关于Nginx对于PHP支持的问题
因为使用的mediawiki是用php写的,所以就遇上了一个问题,怎么让Nginx支持php,记得当时使用apache2的时候是安装了一个叫 “ libapache2-mod-php5 ”的mod , ...
- Jquery 日期差函数 修改 对火狐进行兼容
function DateDiff(sDate1, sDate2) { //sDate1和sDate2是yyyy-MM-dd格式 var aDate, oDate1, oDate2, iDays; a ...
- Winform中Checkbox与其他集合列表类型之间进行关联
本文提供了Checkbox与CheckedListBox.DataGridViewCheckBoxColumn等的联动关系 1.CheckboxAssociateFactroy.Create创建联动关 ...
- 掌握 Azure 的注册、帐户和订阅管理 Azure 上云须知
计划使用由世纪互联运营的 Microsoft Azure 的用户,可通过下列流程注册开通并购买所需 Azure 服务:信息获取 > 试用 > 购买 > 账户/订阅管理 > 支付 ...
- Redis相关注意事项
本文介绍了五个使用Redis使用时的注意事项.如果你在使用或者考虑使用Redis,你可以学习一下下面的一些建议,避免遇到以下提到的问题. 一.配置相关注意事项 1.涉及到内存的单位注意添加 b 1k ...
- Bezier(贝塞尔曲线)
CDC::PolyBezierBOOL PolyBezier( const POINT* lpPoints, int nCount ); 和 曲线原理及多段曲线连接处如何光滑连接:第一段曲线要有4个点 ...
- cookie和session是否可以保存对象
session看了一下,是可以保存对象的.语法很普通,但是cookie的话本身是只能保存string类型的信息的,这就需要先序列化,然后接收的页面反序列化后形成对象调用,为了防止乱码,需要在数据传输的 ...
- [学习笔记] C++ 历年试题解析(三)--小补充
小小的补充一下吧,因为李老师又把直招的卷子发出来了.. 题目 1.有指针变量定义及初始化int *p=new int[10];执行delete [] p;操作将结束指针变量p的生命期.(×) 解释:试 ...
- 【转】IOS开发—IOS 8 中设置applicationIconBadgeNumber和消息推送
在IOS7中设置applicationIconBadgeNumber不会有什么问题,但是直接在IOS8中设置applicationIconBadgeNumber会报错 因为在IOS8中要想设置appl ...
- Bootstrap历练实例:动画的进度条
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...