【leetcode】Intersection of Two Linked Lists(easy)
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.
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.
思路:简单题 遍历长度 走差值 再同步走 答案里给了个转圈圈的思路 感觉没有自己的好 太乱了
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if(headA == NULL || headB == NULL)
{
return NULL;
}
int lenA = ;
int lenB = ;
ListNode * pa = headA;
ListNode * pb = headB;
while(pa->next != NULL)
{
pa = pa->next;
lenA++;
}
while(pb->next != NULL)
{
pb = pb->next;
lenB++;
}
if(pa != pb) return NULL;
pa = headA;
pb = headB;
while(lenA > lenB)
{
pa = pa->next; lenA--;
}
while(lenB > lenA)
{
pb = pb->next; lenB--;
}
while(pa != pb)
{
pa = pa->next;
pb = pb->next;
}
}
【leetcode】Intersection of Two Linked Lists(easy)的更多相关文章
- 【leetcode】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(相交链表)
这道题是LeetCode里的第160道题. 题目讲的: 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, ...
- LeetCode--LinkedList--160. Intersection of Two Linked Lists(Easy)
160. Intersection of Two Linked Lists(Easy) 题目地址https://leetcode.com/problems/intersection-of-two-li ...
- [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][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 (两个链表的交点)
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 ...
随机推荐
- Java开源 开源工作流
OpenEbXML 点击次数7801 Werkflow 点击次数11181 OSWorkflow 点击次数14988 wfmOpen 点击次数7997 OFBiz 点击次数1234 ...
- 实例介绍Cocos2d-x物理引擎:碰撞检测
碰撞检测是使用物理引擎的一个重要目的,使用物理引擎可以进行精确的碰撞检测,而且执行的效率也很高.在Cocos2d-x 3.x中使用事件派发机制管理碰撞事件,EventListenerPhysicsCo ...
- Swift常量和变量以及命名规范
我们在上一章中介绍了如何使用Swift编写一个HelloWorld小程序,其中就用到了变量.常量和变量是构成表达式的重要组成部分.常量在声明和初始化变量时,在标识符的前面加上关键字let,就可以把该变 ...
- CodeBlocks对C++模板的支持
坦率的说CodeBlocks是一款不错的跨平台编译器,一般编写C/C++都是使用它,但最近在编写C++模板文件时,发现它对模板的支持并不是很好.具体表现在模板的定义与声明分开的问题上. 一般编写C/C ...
- IOCP模型总结(转)
IOCP模型总结(转) IOCP(I/O Completion Port,I/O完成端口)是性能最好的一种I/O模型.它是应用程序使用线程池处理异步I/O请求的一种机制.在处理多个并发的异步I/O请求 ...
- Transact-SQL 存储过程(c#调用执行)
1. Microsoft SQL Server Management Studio 中创建 存储过程 1.1 借助模板资源管理器中的Stored Procedure模板进行修改创建 1.2 直接新建查 ...
- 服务器迁移之debian重新配置Web服务的细节
之前配置Linux服务器时采用的是Debian系统一直很稳定,这次准备迁移到新的服务器环境上,好在以前的配置我在博客都做了备忘,所以很容易就搞定了,这次服务系统采用的是最新的Debian 7.0,但是 ...
- php取整的几种方法
php取整的几种方式. floor 舍去法取整 语法格式:float floor ( float value )返回不大于value 的下一个整数,将value 的小数部分舍去取整.floor() 返 ...
- PHP实现冒泡算法
<?php //php函数:count($arr)返回array的数值总数. function bubble_sort($arr){ for ($i = 6;$i > 0;$i --){ ...
- webstorm ftp发布问题
通过webstorm发布遇到问题 Invalid descendent file name "/". 解决方案为 点击[Advanced options]勾选[always use ...