题意:

  给两个链表,他们的后部分可能有公共点。请返回第一个公共节点的地址?若不重叠就返回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. ARCGIS 10中添加excel点字段生产点shp文件的工具

    菜单栏中——文件——添加数据——添加XY数据——选择excel。 选择x,y和投影(注意这里投影应该是原数据的本身投影,不能直接选择你要转换的投影)完成。

  2. Csharp volatile 关键字

    volatile 关键字指示一个字段可以由多个同时执行的线程修改.声明为 volatile 的字段不受编译器优化(假定由单个线程访问)的限制.这样可以确保该字段在任何时间呈现的都是最新的值. vola ...

  3. having——至少被订购过两回的订单

    此篇介绍having的用法 一.表:订单表,产品表 说明:订单表order ,包含prodectid 二.查询至少被订购过两回的订单 800x600 Normal 0 7.8 磅 0 2 false ...

  4. XCode6.1中的ios7.1适配

    在xcode6.1中新创建的项目,运行在我的ios7.1的ipod touch上时(与5s的一样的尺寸, Retina屏幕), 上下出现了黑边,由于没有下载7.1的模拟器,不知道模拟器上有无问题, 查 ...

  5. 三年PS经验

  6. SSH架构简单总结

    Struts.spring.Hibernate在各层的作用 1)struts 负责 web层.    ActionFormBean 接收网页中表单提交的数据,然后通过Action 进行处理,再Forw ...

  7. ORACLE 更新关联多张表

    UPDATE T_XMLC_BILL_HEAD_BAK T1 SET (T1.SENDRECEIVEFLAG, T1.SENDRECEIVEOPERATOR, T1.SENDRECEIVEDATE, ...

  8. ubuntu无线上网静态ip配置以及配置静态IP 之后无法正常上网的解决方案

    一. 配置无线网络的静态IP 编辑/etc/network/interfaces文件如下: auto lo wlan0 iface lo inet loopback iface wlan0 inet ...

  9. 从iPhone4、iPhone5、iPhone6看手机外壳加工工艺进化史

    从iPhone4.iPhone5到iPhone6,苹果为我们推出了一代又一代新产品,让我们享受到最新的科技产品.每次不只是配置上的改变,苹果在工艺上也不断改变.下面就阐述一下我对这几款手机在设计和制造 ...

  10. PNG优化/压缩

    我们总是希望 PNG 图像的容量能够小些.小些.再小些.优化 PNG 图像,可以用以下几个工具: 1.Optipng Optipng 是命令行工具,直接在其后追加所需优化的 PNG 图像即可 2.Pn ...