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.

只需要将listB首位相连,这样listA和listB就形成了一个带有环的链表。

这样题目就变成了Linked List Cycle II

 1 ListNode *getIntersectionNode(ListNode *headA, ListNode *headB)
 2     {
 3         ListNode *tail, *join;
 4         if (headA == NULL || headB == NULL)
 5             return NULL;
 6
 7         for (tail = headB; tail->next != NULL; tail = tail->next); // reach tail
 8         tail->next = headB; // create a circle
 9         join = detectCycle(headA);
10         tail->next = NULL; // keep list B as original
11
12         return join;
13     }
14
15     ListNode *detectCycle(ListNode *head)
16     {
17         ListNode *fast = head, *slow = head;
18
19         while (fast != NULL)
20         {
21             slow = slow->next;
22             fast = fast->next;
23             if (fast != NULL)
24                 fast = fast->next;
25             if (fast != NULL && fast == slow) // catch slow
26                 break;
27         }
28
29         if (fast == NULL)
30             return NULL;
31
32         slow = head;
33         while (slow != fast)
34         {
35             slow = slow->next;
36             fast = fast->next;
37         }
38         return slow;
39     }

转载于:https://www.cnblogs.com/ym65536/p/4141555.html

leetcode. Intersection of Two Linked Lists相关推荐

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

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

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

  4. Leetcode:Intersection of Two Linked Lists

    题目大意:找两个链表的第一个交点. 这里先给出一些链表相交基础的说明: 对于链表X,用X[i]表示链表X的第i个元素,L(X)表示X的长度. 性质1:如果X[i]等于Y[j],那么可以推出X[i+1] ...

  5. LeetCode(160): Intersection of Two Linked Lists

    Intersection of Two Linked Lists: Write a program to find the node at which the intersection of two ...

  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. C#LeetCode刷题之#160-相交链表(Intersection of Two Linked Lists)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3824 访问. 编写一个程序,找到两个单链表相交的起始节点. 例如 ...

  8. leetcode python3 简单题160. Intersection of Two Linked Lists

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百六十题 (1)题目 英文: Write a program to find t ...

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

最新文章

  1. 高难度的c语言程序例子,高难度脑筋急转弯分享
  2. 北大女生拿下阿里数学预赛第一名!决赛入围率不到1%,最小晋级选手只有14岁...
  3. EasyPR--开发详解(8)文字定位
  4. Dynamips/Dynagen模拟CISCO路由环境
  5. 动态添加html元素绑定事件,关于javascript:jQuery如何将onclick事件绑定到动态添加的HTML元素...
  6. centos7下python3与python2共存并且开启py3虚拟环境
  7. 人群频率 | gnomAD数据库简介 (一)
  8. 网站性能优化 - 数据库及服务器架构篇
  9. DSP28335学习记录(二)——外部中断和定时器中断
  10. 从 ftp 上下载文件、文件夹
  11. 后台管理系统----day03
  12. 一维数组实验题:大奖赛现场统分。已知某大奖赛有n个选手参赛,m(m>2)个评委为参赛选手评分(最高10分,最低0分)。统分规则为:在每个选手的m个得分中,去掉一个最高分和一个最低分后,取平均分作为该选
  13. 安全测试(六)iOS ipa软件安全 APP应用安全 手机软件安全 ipa安全 ipa反编译 应用日志窃取 ipa漏洞 应用软件本身功能漏洞 iPhone移动应用常规安全讲解
  14. 一文读懂nginx gzip_static
  15. 关于企业邮箱域名备案方法【企业邮箱申请】
  16. Exception】Chrome浏览器提示:此网页正试图从未经验证的来源加载脚本
  17. 计算机组成原理 定点加法、减法 练习题
  18. Tomcat服务器配置https双向认证,使用JDK的keytool生成证书(适用于web、安卓、IOS)
  19. 一个屌丝脱屌的经历【转载】
  20. (一)同步与异步的相关概念

热门文章

  1. DATEDIFF() 函数返回两个日期之间的天数
  2. css 语音,纯CSS打造(无图像无js)的非常流行的讲话(语音)气泡效果
  3. java检查变量是否定义_JavaScript检查变量是否存在(已定义/初始化)
  4. 计算机发展史的内容概述,计算机及其发展史概述
  5. 计算机题硬盘分区首先,您对计算机硬盘分区了解多少: 如何进行分区合理?
  6. linux邮件加密码,linux系统的邮件服务器的加密与验证
  7. NYOJ-会场安排问题(贪心)
  8. Android学习笔记(五)——RadioGroupRadioButton
  9. OCR技术系列实践:银行卡、身份证、门牌号、护照、车牌、印刷体汉字识别
  10. Qt官网变更【2012】