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.

开始想道用栈来做,先入栈,然后出栈比较直接就出结果了,但是题目说只能利用一个空间,所以栈的方法不行了。

看网上的答案,没想到这么简单,暴力解法,什么算法、数据结构都没用,题刷多了,脑袋都僵了,一直以为要用什么算法来做呢。。。。

思路:

查找两个链表的第一个公共节点,如果两个节点的尾节点相同,肯定存在公共节点

方法: 长的链表开始多走 (h1的数量 - h2的数量)步,然后和短链表同步往下走,遇到的第一个相同的节点就是最早的公共节点

/**
* 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==NULL||headB==NULL)
return NULL;
ListNode *flagA=headA;
ListNode *flagB=headB;
int countA=;
int countB=;
while(flagA->next!=NULL)
{
countA++;
flagA=flagA->next;
}
while(flagB->next!=NULL)
{
countB++;
flagB=flagB->next;
}
if(flagA!=flagB)
return NULL;
else
{
int diff=abs(countA- countB);
if(countA>=countB)
{
flagA=headA;
flagB=headB;
}
else
{
flagA=headB;
flagB=headA;
}
while(diff)
{
flagA=flagA->next;
diff--;
}
while(flagA!=flagB)
{
flagA=flagA->next;
flagB=flagB->next;
}
return flagA;
}
}
};

  

Intersection of Two Linked Lists——经典问题的更多相关文章

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

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

  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. 2016.5.24——Intersection of Two Linked Lists

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

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

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

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

  7. LeetCode_160. Intersection of Two Linked Lists

    160. Intersection of Two Linked Lists Easy Write a program to find the node at which the intersectio ...

  8. LeetCode--LinkedList--160. Intersection of Two Linked Lists(Easy)

    160. Intersection of Two Linked Lists(Easy) 题目地址https://leetcode.com/problems/intersection-of-two-li ...

  9. 【原创】leetCodeOj --- Intersection of Two Linked Lists 解题报告(经典的相交链表找交点)

    题目地址: https://oj.leetcode.com/problems/intersection-of-two-linked-lists/ 题目内容: Write a program to fi ...

随机推荐

  1. 算法复习——欧拉回路(uoj117)

    题目: 题解: 欧拉回路相关定理(相关定义和证明请参见其他资料): 1.欧拉回路 (1)有向图:所有点的出度都等于入度为该图为欧拉图(存在欧拉回路)的充要条件. (2)无向图:所有点的度都为偶数为该图 ...

  2. node egg.js使用superagent做文件转发

    使用 egg.js + superagent 进行文件上传转发 // app/controller/file.js const Controller = require('egg').Controll ...

  3. bzoj 1564 [NOI2009]二叉查找树 区间DP

    [NOI2009]二叉查找树 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 906  Solved: 630[Submit][Status][Discu ...

  4. HDU1832 二维线段树求最值(模板)

    Luck and Love Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...

  5. springboot整合thumbnailator实现图片压缩

    springboot整合thumbnailator实现图片压缩 前言 最近由于首页产品列表图片显示太慢,经过研究发现是用户上传的图片太大. 针对这个问题,想到的解决方案是: 1. 产品上传时,限定图片 ...

  6. Data Mining的十种分析方法——摘自《市场研究网络版》谢邦昌教授

    Data Mining的十种分析方法: 记忆基础推理法(Memory-Based Reasoning:MBR)        记忆基础推理法最主要的概念是用已知的案例(case)来预测未来案例的一些属 ...

  7. bzoj 2957: 楼房重建 ——线段树

    Description 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上的房子拆了又建.建了又拆.他经常无聊地看着窗外发呆,数自己能够看到多少栋房子. 为了简化问题,我们考虑这些 ...

  8. bzoj 1004 burnside 引理+DP

    对于burnside引理需要枚举染色,这道题属于burnside的一种简单求解的方法,就是polya,我们可以使每一种置换中的循环节中的元素的颜色都相同,那么这样的话就可以直接DP了,我们可以将m个置 ...

  9. Android应用程序App应用上线流程

    对于很多初级开发者,可能对app应用上线不太了解,本文跟大家介绍一下怎么上线app应用.上线App并不是一件很困难的事情,App的应用功能也不需要很强大,甚至不用联网,只有简单的一两个页面的App应用 ...

  10. Kill windows和linux 进程

    Windows