题意:

  给两个链表,他们的后部分可能有公共点。请返回第一个公共节点的地址?若不重叠就返回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. JavaScript技巧45招

    原文:45 Useful JavaScript Tips, Tricks and Best Practices作者:Saad Mousliki 在这篇文章里,我将分享一些JavaScript的技巧.秘 ...

  2. PHP webserver 之 soap non-wsdl

    non-wsdl 顾名思义就是不使用wsdl文件进行的webserver服务 事实上个人觉得用wsdl模式还不如使用non-wsdl模式,wsdl模式稍加麻烦! 1.网站运行环境下开启soap模块(p ...

  3. PowerDesigner 非数值默认值时会自动增加单引单

    在PowerDesigner中,如果默认值是非数值型的,那么 PowerDesigner 会默认加上单引号 因此我们需要把这个默认的单引号干掉,如果是需要设置字符串默认值的时候,就手工加上 单引号 即 ...

  4. Java类加载器加载类顺序

    java ClassLoader的学习 java是一门解释执行的语言,由开发人员编写好的java源文件先编译成字节码文件.class形式,然后由java虚拟机(JVM)解释执 行,.class字节码文 ...

  5. EXTJS 4.2 资料 控件之Window窗体添加html

    //这里要跳转页面 var subWindow = new Ext.Window({ title: '窗口', width: width, height: height, modal: true,// ...

  6. Gnome 插件介绍

    插件:Applications Menuhttps://extensions.gnome.org/extension/6/applications-menu/ TopIconshttps://exte ...

  7. Object调用静态方法

    谁说空指针不能调用方法 public class Foo { public static void bar() { System.out.println("bar"); } pub ...

  8. winform访问url传参有返回值

    using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Net;usi ...

  9. PAT-乙级-1021. 个位数统计 (15)

    1021. 个位数统计 (15) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 给定一个k位整数N = dk-1 ...

  10. CSRF攻击原理解析与对策研究

    1.引言       跨站点请求伪造(Cross—Site Request Forgery).以下简称CSRF.是一种广泛存在的网站漏洞.Gmail.YouTube等著名网站都有过CSRF漏洞.甚至包 ...