题目

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

For example, the following two linked lists:



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.

分析

给定两个链表,求它们的交叉节点。

要求,时间复杂度在O(n)内,空间复杂度为O(1)

两个链表的长度不定,但是交叉节点的后续节点全部相同,所以先求得每个链表的长度lenA和lenB,将较长的链表先移动|lenA−lenB|个位置,然后同时后移,遇到的第一个值相等的节点既是要求的交叉节点。

AC代码

/**
* 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) {
if (!headA || !headB)
return NULL; ListNode *p = headA, *q = headB; //求出输入两个链表的长度
int lenA = 0, lenB = 0;
while (p)
{
++lenA;
p = p->next;
}//while while (q)
{
++lenB;
q = q->next;
}//while //让长的链表先移动多出的节点
p = headA;
q = headB;
if (lenA > lenB)
{
int i = 0;
while (p && i < lenA - lenB)
{
p = p->next;
++i;
}//while
}
else{
int j = 0;
while (q && j < lenB - lenA)
{
q = q->next;
++j;
}//while
} while (p && q && p->val != q->val)
{
p = p->next;
q = q->next;
}//while return p;
}
};

GitHub测试程序源码

LeetCode (160) Intersection of Two Linked Lists的更多相关文章

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

  2. (LinkedList)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之“链表”:Intersection of Two Linked Lists

    此题扩展:链表有环,如何判断相交? 参考资料: 编程判断两个链表是否相交 面试精选:链表问题集锦 题目链接 题目要求: Write a program to find the node at whic ...

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

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

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

  8. 160. Intersection of Two Linked Lists【easy】

    160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...

  9. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

随机推荐

  1. 浮点数据与IEE754

    在计算机系统(包括单片机)中,浮点数(单精度float和双精度的double)对采用IEE-754标准.该标准为 32 位浮点和 64 位双精度浮点二进制小数定义了二进制标准. IEEE 754 用科 ...

  2. Zeppelin的入门使用系列之使用Zeppelin来创建临时表UserTable(三)

    不多说,直接上干货! 前期博客 Zeppelin的入门使用系列之使用Zeppelin运行shell命令(二) 我们必须要先使用Spark 语句创建临时表UserTable,后续才能使用Spark SQ ...

  3. [代码修订版] Python 踩坑之旅 [进程篇其四] 踩透 uid euid suid gid egid sgid的坑坑洼洼

    目录 1.1 踩坑案例 1.2 填坑解法 1.3 坑位分析 1.4 技术关键字 1.5 坑后思考 下期坑位预告 代码示例支持 平台: Centos 6.3 Python: 2.7.14 代码示例: 公 ...

  4. clickhouse源码Redhat系列机单机版安装踩坑笔记

    前情概要 由于工作需要用到clickhouse, 这里暂不介绍概念,应用场景,谷歌,百度一大把. 将安装过程踩下的坑记录下来备用 ClickHouse源码 git clone安装(直接下载源码包安装失 ...

  5. I/O操做总结(四))

    前面已经把java io的主要操作讲完了 这一节我们来说说关于java io的其他内容 Serializable序列化 实例1:对象的序列化 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

  6. ZR#331. 【18 提高 3】括号序列(栈)

    题意 挺神仙的.首先$60$分暴力是比较好打的. 就是枚举左端点,看右端点能否是$0$ 但是这样肯定是过不了的,假如我们只枚举一次,把得到的栈记录下来 那么若区间$(l, r)$是可行的,那么$s_{ ...

  7. To the world you may be one person, but to one person you may be the world.

    To the world you may be one person, but to one person you may be the world.对于世界而言,你是一个人:但对于某人而言,你是他的 ...

  8. 用jQuery实现jsonp跨域

    跨域的安全限制都是指浏览器端来说的.服务器端是不存在跨域安全限制的,所以通过本机服务器端通过类似httpclient方式完成“跨域访问”的工作,然后在浏览器端用AJAX获取本机服务器端“跨域访问”对应 ...

  9. 织梦后台上传mp4视频不显示

    include/dialog/select_media.php ,约185行, 把(rm|rmvb) 改为(rm|mp4|rmvb)

  10. 深入理解Java虚拟机--个人总结

    JVM内存区域 我们在编写程序时,经常会遇到OOM(out of Memory)以及内存泄漏等问题.为了避免出现这些问题,我们首先必须对JVM的内存划分有个具体的认识.JVM将内存主要划分为:方法区. ...