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)的更多相关文章

  1. 【leetcode】Intersection of Two Linked Lists

    题目简述: Write a program to find the node at which the intersection of two singly linked lists begins. ...

  2. 【LeetCode】Intersection of Two Linked Lists(相交链表)

    这道题是LeetCode里的第160道题. 题目讲的: 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, ...

  3. LeetCode--LinkedList--160. Intersection of Two Linked Lists(Easy)

    160. Intersection of Two Linked Lists(Easy) 题目地址https://leetcode.com/problems/intersection-of-two-li ...

  4. [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 ...

  5. [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 ...

  6. 【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 ...

  7. [leetCode][003] Intersection of Two Linked Lists

    [题目]: Write a program to find the node at which the intersection of two singly linked lists begins. ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. Cocos2d-x中自定义粒子系统

    除了使用Cocos2d-x的11种内置粒子系统外,我们还可以通过创建ParticleSystemQuad对象,并设置属性实现自定义粒子系统,通过这种方式完全可以实现我们说需要的各种效果的粒子系统.使用 ...

  2. 手机看youtube的方法|一个好用的VPN

    俗话说:网络无国界.但是由于天朝的限制,我们无法访问YouTube .谷歌.twitter等一些国外网站. 这个时候我们就需要FQ.最直接有效的方法就是:VPN(虚拟专用网络). 虚拟专用网络的功能是 ...

  3. 绝对URL和相对URL

    什么是URL? 应用举例:可以是图片等资源地址,浏览器地址栏的网址等等 Uniform Resource Locator 统一资源定位符 http://www.123.com/infor/index. ...

  4. httpc服务器错误类型大全

    HTTP 400 - 请求无效HTTP 401.1 - 未授权:登录失败HTTP 401.2 - 未授权:服务器配置问题导致登录失败HTTP 401.3 - ACL 禁止访问资源HTTP 401.4 ...

  5. C# 导出 Excel

    /// <summary> /// 导出Excel /// </summary> public void ExportExcel() { #region 添加引用 Micros ...

  6. Unity学习笔记(2):注册映射

    在上一篇文章中(认识Unity)中概要介绍了Unity和Ioc,本节主要介绍IoC中的注册映射,并使用代码和配置文件两种方式进行说明. 定义依赖注入相关信息 定义ILogger接口 public in ...

  7. [大牛翻译系列]Hadoop系列性能部分完结

    Hadoop系列性能部分完结.其它的部分发布时间待定. Hadoop系列将不再一日一篇,开始不定期发布.

  8. php异步加载、多线程fsockopen()、fputs()

    index.php <?php function test() { $fp=fsockopen("localhost", 80, $errno, $errstr, 30); ...

  9. sqlsever2008及以上各个安装包的说明

    LocalDB (SqlLocalDB)LocalDB 是 Express 的一种轻型版本,该版本具备所有可编程性功能,但在用户模式下运行,并且具有快速的零配置安装和必备组件要求较少的特点.如果您需要 ...

  10. 如何在eclipse中配置Selenium

    1, Install python 33.(Python 27也可以) 2, Setup Selenium If you did not install Easy_install module, yo ...