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.

 1 class Solution {
 2 public:
 3     ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
 4         if(headA==NULL||headB==NULL) return NULL;
 5
 6         int lenA=1;
 7         ListNode * curA=headA;
 8         while(curA->next)
 9         {
10             lenA++;
11             curA=curA->next;
12         }
13
14         int lenB=1;
15         ListNode * curB=headB;
16         while(curB->next)
17         {
18             lenB++;
19             curB=curB->next;
20         }
21
22         if(curA!=curB)
23             return NULL;
24         else
25         {
26             int diff=lenA-lenB;
27             if(diff>0)
28             {
29                 while(diff)
30                 {
31                     headA=headA->next;
32                     diff--;
33                 }
34             }
35             else
36             {
37                 while(diff)
38                 {
39                     headB=headB->next;
40                     diff++;
41                 }
42             }
43
44             while(1)
45             {
46                 if(headA==headB)
47                     return headA;
48                 else
49                 {
50                     headA=headA->next;
51                     headB=headB->next;
52                 }
53             }
54         }
55     }
56 };

转载于:https://www.cnblogs.com/jawiezhu/p/4504231.html

【leetcode】Intersection of Two Linked Lists相关推荐

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

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

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

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

  5. 【LeetCode】328. Odd Even Linked List 解题报告(Python C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 【leetcode】86. Partition List

    题目如下: Given a linked list and a value x, partition it such that all nodes less than x come before no ...

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

  8. 【leetcode】109. Convert Sorted List to Binary Search Tree

    题目如下: Given a singly linked list where elements are sorted in ascending order, convert it to a heigh ...

  9. 【LeetCode】【HOT】23. 合并K个升序链表(递归+分治)

    [LeetCode][HOT]23. 合并K个升序链表 文章目录 [LeetCode][HOT]23. 合并K个升序链表 package hot;import java.util.Arrays;cla ...

最新文章

  1. iis应用程序池不能启动
  2. 鸿蒙系统系列教程5-鸿蒙开发环境的搭建
  3. Boost:异步操作,涉及重新打包多个操作,但选择仅调用其中一个的测试程序
  4. 微服务 边界服务_遵循这些实用原则以获取精心设计的微服务边界
  5. oracle 11g 使用图解,oracle 11g adrci 工具使用方法
  6. oracle存储过程中数组的使用
  7. BZOJ 2406 LuoguP4194 矩阵 有上下界可行流
  8. SerializeField和Serializable
  9. ftp服务器文件端口,ftp服务器端口用哪个文件
  10. 一dubbo框架学前原理介绍
  11. VS编译器的简单操作
  12. dosbox 中文操作系统_中兴新支点国产操作系统新版本了,越来越好用了
  13. 该怎么复习安徽省考计算机专业课
  14. 树莓派 pico pio 可编程io
  15. 电脑术语中:directory 和 folder 的区别
  16. layui 之动态改变表格中单元格的背景色
  17. Android支持播mp4的文件管理,Android - 简单使用VideoView播放MP4
  18. mulesoft Module 10 quiz 解析
  19. 等式约束与不等式约束问题
  20. 【Python 实战基础】 如何绘制中国地图展示省份GDP数据

热门文章

  1. POJ 1160 Post Office
  2. linux服务之nagios
  3. Microsoft和AWS推出免费的云优化服务
  4. DPM 2010(三)---Exchange2010单邮箱恢复
  5. javascript对XMLHttpRequest异步请求的面向对象封装
  6. 取消任务栏中又出现了红色的盾牌
  7. 如何改android device monitor文件的权限
  8. AndroidStudio 生成Jar并混淆
  9. 一个一元二次方程求解编程引申的两个知识点(abs和fabs的区别以及浮点数比较相等)...
  10. 后盾网lavarel视频项目---lavarel多表关联一对多操作实例