[抄题]:

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.

[暴力解法]:

时间分析:

空间分析:

[思维问题]:

节点相交不是节点相等,可见读题很重要。节点是否相等用等号即可。

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 统计链表长度的时候,length要加,但是node不能忘了动,都要写
  2. 二者齐头并进之后,不需要加提前退出的corner case,就算没有也是最后才退出。应该想好

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

读懂题目很重要

[复杂度]:Time complexity: O(n) Space complexity: O(1)

还是原来的链表,缩短 扫一遍 返回,没有新建别的链表。符合就地取材原则,所以是1

[英文数据结构或算法,为什么不用别的数据结构或算法]:

两个for循环:可以直接判断

[关键模板化代码]:

//getLength
public int getLength(ListNode node) {
int length = 0;
while(node != null) {
length++;
node = node.next;
}

链表while(node != null)取长度

[其他解法]:

炫耀技巧,没必要

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/ public class Solution {
/*
* @param headA: the first list
* @param headB: the second list
* @return: a ListNode
*/
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
//corner case
if (headA == null || headB == null) {
return null;
}
//keep the same length
int A_len = getLength(headA);
int B_len = getLength(headB); while (A_len > B_len) {
headA = headA.next;
A_len--;
}
while (A_len < B_len) {
headB = headB.next;
B_len--;
}
//find the same node
while (headA != headB) {
headA = headA.next;
headB = headB.next;
} return headA;
} //getLength
public int getLength(ListNode node) {
int length = 0;
while(node != null) {
length++;
node = node.next;
}
return length;
}
}

两个链表的交叉 · Intersection of Two Linked Lists的更多相关文章

  1. [LeetCode]求两个链表的焦点--Intersection of Two Linked Lists

    标题题目地址 1.解题意 求解两个链表的焦点,这个交点并不是焦点的值相等,而是需要交点之后的数据是完全相等的. 落实到java层面,就是交点处的对象是同一个对象即可. ps:我最开始没有读懂题目,然后 ...

  2. 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. lintcode 中等题:Intersection of Two Linked Lists 两个链表的交叉

    题目 两个链表的交叉 请写一个程序,找到两个单链表最开始的交叉节点. 样例 下列两个链表: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 在节点 c1 开始交 ...

  4. LintCode-380.两个链表的交叉

    两个链表的交叉 请写一个程序,找到两个单链表最开始的交叉节点. 注意事项 如果两个链表没有交叉,返回null. 在返回结果后,两个链表仍须保持原有的结构. 可假定整个链表结构中没有循环. 样例 下列两 ...

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

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

  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】#160. Intersection of Two Linked Lists

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...

  8. 2016.5.24——Intersection of Two Linked Lists

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

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

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

随机推荐

  1. 7-13 求链式线性表的倒数第K项(20 分)

    给定一系列正整数,请设计一个尽可能高效的算法,查找倒数第K个位置上的数字. 输入格式: 输入首先给出一个正整数K,随后是若干正整数,最后以一个负整数表示结尾(该负数不算在序列内,不要处理). 输出格式 ...

  2. 在C#中调用Java生成的jar包文件的方法

    C#工程调用Java已生成的jar包步骤如下: 一.使用IKVM.NET组件 首先到IKVM官网(http://www.ikvm.net)下载组件,下载地址:https://sourceforge.n ...

  3. java之美妙的注解

    注解 一.认识注解 先看百度百科对java注解的解释: 定义:注解(Annotation),也叫元数据.一种代码级别的说明.它是JDK1.5及以后版本引入的一个特性,与类.接口.枚举是在同一个层次.它 ...

  4. koa/koa2项目搭建

    一键生成koa/koa2项目: 1. npm install -g koa-generator 2.新建项目目录 koa mytest (koa1项目) koa2 koa2test (koa2项目) ...

  5. gqlgen golang graphql server 基本试用

    gqlgen golang 的graphql server 具体代码参考https://github.com/rongfengliang/gqlgen-demo 特点 模型优先 类型安全 代码生成 安 ...

  6. IT项目管理工具总结

    IT项目管理工具总结 俗话说"工欲善其事必先利其器",在一个项目开发流程中,如果搭配一个比较完善的项目管理工具,必将取得事半功倍的效果.本文搜集了目前项目管理界比较有规模的管理工具 ...

  7. Centos 6 安装 配置 oracle11g R2

    1.安装centos6.3_64位: 下载地址:http://mirror.bit.edu.cn/centos/6.3/isos/x86_64/ CentOS-6.3-x86_64-bin-DVD1. ...

  8. php的静态变量的实现

    1.静态变量的结构 php脚本编译之后会生成执行opcode组成的opcode_array,执行每个zend_op_array都会生成一个单独的zend_execute_data结构. php的局部变 ...

  9. java 面向对象 — 封装

      

  10. Python将数据写入excel或者txt,读入csv格式或xls文件,写入csv(写一行空一行解决办法)

    1.写入excel,一开始不需要自己新建一个excel,会自动生成 attribute_proba是我写入的对象 import xlwt myexcel = xlwt.Workbook() sheet ...