题意:

  给两个链表,他们的后部分可能有公共点。请返回第一个公共节点的地址?若不重叠就返回null。

思路:

  用时间O(n)和空间O(1)的做法。此题数据弱有些弱。

  方法(1)假设两个链表A和B,用两个指针分别按顺序遍历AB和BA,这AB和BA肯定等长的。如果他们有公共点,那么按照这样走必定会在某个点相遇。所以只要预先判断是否有公共点,这只需要用两个指针分别指向A和B的链尾判断是否相同即可(这一步可以使用更简单的方法,增加1个计数器,判断指针1是否已经遍历过AB了)。

 /**
* 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) {
ListNode *L1=headA;
ListNode *L2=headB;
//若无交点,且AB等长,那么他们会在null处相遇,退出。
//先判断是否有交点先。
while(L1&&L1->next) L1=L1->next;
while(L2&&L2->next) L2=L2->next;
if(L1!=L2) return NULL;
L1=headA;
L2=headB;
while(L1!=L2)
{
if(L1) L1=L1->next;
else L1=headB; if(L2) L2=L2->next;
else L2=headA;
}
return L1;
}
};

AC代码

  方法(2)先统计链A和B的长度,假设A长,那么指针1先走|A|-|B|步,然后再同时走,若有公共点必定会相遇。

LeetCode Intersection of Two Linked Lists (找交叉点)的更多相关文章

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

  2. LeetCode Intersection of Two Linked Lists

    原题链接在这里:https://leetcode.com/problems/intersection-of-two-linked-lists/ 思路:1. 找到距离各自tail 相同距离的起始List ...

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

  4. LeetCode——Intersection of Two Linked Lists

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

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

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

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

  8. 2016.5.24——Intersection of Two Linked Lists

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

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

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

随机推荐

  1. [Learn Android Studio 汉化教程]第三章:使用 Android Studio 编程

    [Learn Android Studio 汉化教程]第三章:使用 Android Studio 编程 本章包含如何在 Android Studio 中书写或生成代码. Android Studio ...

  2. ContextMenuStrip控件

    设置单击窗体右键菜单 注意如果想在form1中显示右键菜单,那么要设置form1的属性

  3. 【学习总结】【多线程】 多线程概要 & GDC & NSOperation

    基本需要知道的 :  进程 :  简单点来说就是,操作系统中正在运行的一个应用程序,每个进程之间是独立的,每个进程均运行在受保护的内存空间内 线程 :  一个进程(进程)想执行任务,必须有线程(所以, ...

  4. 演示demo开发问题及解决方案集锦

    模型处理问题: 1. 3Dmax模型导入Unity单位设置: 自定义->单位设置->系统单位设置与显示单位比例都调成厘米 2. 3Dmax中材质贴图: 点击材质编辑器[在模式下可以选择精简 ...

  5. First Groovy

    class Sample { def names = ["anna", "annie", "tommy", "bobby" ...

  6. xcode 上 crash 调试的三种方法

    最近有新人问crash调试方法,简介记录如下: 模拟器调试 打开控制台查看输出日志 显示出错的行数 显示出错的函数iOS Crash跟踪 真机调试 首先修改真机调试的 bundle ID,使代码可以进 ...

  7. jquery图片放大器插件

    将鼠标移动到一张图片上来的时候,放大该图片的某些细节. <html> <head> <script src="../js/jquery-1.6.js" ...

  8. 中国首个 SaaS 模式的云告警平台安卓版 APP 上线

    今年一月底,国内首个 SaaS 模式的云告警平台 OneAlert 正式发布了 iOS 版 App 客户端,今天上午,安卓版 App 客户端也正式上线了!每个安卓用户,无需电脑,都可以通过手机全程跟踪 ...

  9. ECNU-2574 Principles of Compiler

    题意: 给出编译规则,求是否满足条件 A:= '(' B')'|'x'.    B:=AC.    C:={'+'A}. 其中{}表示里面的内容可以出现0次或者多次 注意点见代码注释 #include ...

  10. HDU4602+推导公式

    手动列出前5项 可发现规律 /* 推导公式 a[n] = 2^(n-1) + (n-2)*2^(n-3) */ #include<stdio.h> #include<math.h&g ...