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.
/**
* 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) {
<span style="white-space:pre"> </span>int lenA = 0;
int lenB = 0;
ListNode *head1 = headA;
<span style="white-space:pre"> </span>ListNode *head2 = headB;
while (head1!=NULL)
{
head1 = head1->next;
lenA++;
}
while (head2!=NULL)
{
head2 = head2->next;
lenB++;
}
int n;
if (lenA < lenB)
{
n = lenB - lenA;
head1 = headA;
head2 = headB;
for (int i = 0; i < n; i++)
{
head2 = head2->next;
}
}
else
{
n = lenA - lenB;
head1 = headA;
head2 = headB;
for (int i = 0; i < n; i++)
{
head1 = head1->next;
}
} while (head1 != head2)
{
head1 = head1->next;
head2 = head2->next;
} return head1;
}
};




Leetcode45: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 whic ...

  3. LeetCode OJ: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. lintcode 中等题:Intersection of Two Linked Lists 两个链表的交叉

    题目 两个链表的交叉 请写一个程序,找到两个单链表最开始的交叉节点. 样例 下列两个链表: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 在节点 c1 开始交 ...

  5. [LeetCode 题解]:Intersection of Two Linked Lists

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Suppose an ...

  6. 2016.5.24——Intersection of Two Linked Lists

    Intersection of Two Linked Lists 本题收获: 1.链表的输入输出 2.交叉链表:这个链表可以有交叉点,只要前一个节点的的->next相同即可. 题目:Inters ...

  7. [LintCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...

  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: Intersection of Two Linked Lists 解题报告

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

随机推荐

  1. 「LOJ10150」括号配对

    [题目] Hecy 又接了个新任务:BE 处理.BE 中有一类被称为 GBE. 以下是 GBE 的定义: 空表达式是 GBE 如果表达式 A 是 GBE,则 [A] 与 (A) 都是 GBE 如果 A ...

  2. T - Amusing Joke(map)

    Problem description So, the New Year holidays are over. Santa Claus and his colleagues can take a re ...

  3. wcf 错误:无法加载或初始化请求的服务提供程序

    解决办法:cmd netsh winsock reset 恢复网络编程接口

  4. collectionView必须点击两次才跳转

    今天遇到一个很奇怪的现象:collectionView必须点击两次才能跳转.具体看代码: -(void)collectionView:(UICollectionView *)collectionVie ...

  5. php获取当地时间 time zone

      PHP5.2.4之前的版本无需设置时区.下面是修改PHP时区的三个办法. 1.修改PHP.ini这个文件 找到date.timezone这行,去掉前面的分号,改成: Java代码 date.tim ...

  6. Go Server Record

    Centos Setup Go yum install glibc-headers gcc-c++ # 一键安装shadowsocks 1. wget --no-check-certificate - ...

  7. 查看SqlServer连接所使用的端口号

    最近一个项目里用到了一个插件,在配置时发现连接数据库使用的是JDBC,输入URL时用到了端口号.印象中在使用Sqlserver时貌似没有提到端口号,在网上查阅了一下,记录下来省的忘了 方法是通过内置的 ...

  8. css3基础篇一

    CSS3 选择器 选择器 例子 例子描述 CSS .class .intro 选择 class="intro" 的所有元素. 1 #id #firstname 选择 id=&quo ...

  9. asp.net mvc学习入门

    MVC是什么? M: Model就是我们获取的网页需要的数据 V: View就是我们的aspx页面,注意这是一个不包含后台代码文件的aspx页面.(其实带有.asp.cs文件也不会有编译错误,但是这样 ...

  10. JQ 获取下一个元素和获取下一个元素的[指定]子元素

    <script type="text/javascript"> $(function () { $("#div1").next().addClass ...