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. Hive搭建与简单使用

    hive搭建与简单使用(1) 标签(空格分隔): hive,mysql hive相当于编译器的组件,他并不存储数据,元数据存储在mysql中,数据则存放在hdfs中,通过hive,可以利用sql语句对 ...

  2. 【DP】编辑距离

    日常吐槽:关于DP,有一种莫名的恐惧...maybe源于与mtw大佬与quantum11大佬,初中时抬老师爬楼梯的经历... 言归正传: 编辑距离 [题目描述] 设A和B是两个字符串.我们要用最少的字 ...

  3. 取消VS2017窗口置顶

    今天打开VS2017,莫名其妙窗口置顶了,百度了一下如何取消窗口置顶,就是Ctrl+Alt+Esc组合键,就可以取消窗口置顶了,至于到底怎么会突然置顶的我也不知道emmm... /********** ...

  4. 移动端弹性滑动以及vue记录滑动位置

    -webkit-overflow-scrolling介绍 -webkit-overflow-scrolling: auto | touch; auto: 普通滚动,当手指从触摸屏上移开,滚动立即停止 ...

  5. html与css命名规范小结

    一.命名规则说明 所有的命名最好都用小写 使用英文命名 给每一个表格和表单加上一个唯一的.结构标记id 给每个图片加上alt标签,优点在于图片发生错误时,alt可以体现给用户 二.相对网页外层重要部分 ...

  6. 大白话理解箭头函数this

    var obj1={ num:4, fn:function(){ num:5; var f=() => { num:6; console.log(this.num); //4 外层非箭头函数包裹 ...

  7. android黑科技系列——分析某直播App的协议加密原理以及调用加密方法进行协议参数构造

    一.前言 随着直播技术火爆之后,各家都出了直播app,早期直播app的各种请求协议的参数信息都没有做任何加密措施,但是慢慢的有人开始利用这个后门开始弄刷粉关注工具,可以让一个新生的小花旦分分钟变成网红 ...

  8. Hibernate_01_初体验

    hibernate开发的基本步骤: 编写配置文档hibernate.cfg.xml: 编写实体类: 生成对应实体类的映射文件并添加到配置文档中: 调用hibernate API进行测试. Hibern ...

  9. End to End Sequence Labeling via Bi-directional LSTM CNNs CRF

    来看看今日头条首席科学家的论文: End-to-end Sequence Labeling via Bi-directional LSTM-CNNs-CRF 使用LSTM方法进行序列标注,完成大规模标 ...

  10. MxNet教程:使用一台机器训练1400万张图片

    官网链接:http://mxnet.readthedocs.io/en/latest/tutorials/imagenet_full.html Training Deep Net on 14 Mill ...