听歌曲初爱有感:

开头啰嗦两句,刚在做算法题目的时候,听到了杨宗纬的《初爱》,突然有了一种本科时候的感觉,想想自己现在研二了,青春喂了狗,我果断喝了一罐啤酒,循环这首歌到吐…..

题目:

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.

思路:

  • 首先这道题目的意思是判断两条链表是不是有重叠的部分,如果有返回那个交叉的节点,如果没有返回null
  • 这道题目的思路是:如果有交叉,最后肯定有相同的部分,如上面那样
  • 举个例子,a和b两头链条,a.length = 7,b.length = 5,用两个指针listA和listB,分别指向a和b的head,listA往后两个,开始listA和listB同步往后移动开始比较,有相同的就有交叉,返回那个节点
  • -

代码:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode a = headA;;
        ListNode b = headB;;
        int lengthA = 0;
        int lengthB = 0;
        int n = 0;
        while(a != null){
            a = a.next;
            lengthA++;
        }
        while(b != null){
            b = b.next;
            lengthB++;
        }
        if(lengthA > lengthB){
            n = lengthA - lengthB;
             a = headA;
             b = headB;
            while( n > 0){
                n--;
                a = a.next;
            }
        }else{
            n = lengthB -lengthA;
            a = headA;
            b = headB;
            while(n > 0){
                n--;
                b = b.next;
            }
        }
        while(a != b){
            a = a.next;
            b = b.next;
        }
        return a;
    }
}

LeetCode(39)-Intersection of Two Linked Lists的更多相关文章

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

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

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

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

  5. 【leetcode】Intersection of Two Linked Lists

    题目简述: Write a program to find the node at which the intersection of two singly linked lists begins. ...

  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. Java for 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 ...

  8. [leetCode][003] Intersection of Two Linked Lists

    [题目]: Write a program to find the node at which the intersection of two singly linked lists begins. ...

  9. ✡ leetcode 160. Intersection of Two Linked Lists 求两个链表的起始重复位置 --------- java

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

随机推荐

  1. 剑指Offer——乐视笔试题+知识点总结

    剑指Offer--乐视笔试题+知识点总结 情景回顾 时间:2016.9.19 15:10-17:10 地点:山东省网络环境智能计算技术重点实验室 事件:乐视笔试   总体来说,乐视笔试内容体量不算少, ...

  2. Centos7安装Tair及配置测试

    系统环境 Centos7 64位 外网ip 182.254.145.66 内网ip 10.105.23.114 安装位置 /usr/local/tair Tair介绍 参见官网 安装 想了半天,我还是 ...

  3. 05_MyBatis基于注解的开发

     要想开发基于注解的MyBatis应用.需要先写一个带有注解的接口. PersonDao.java的写法如下: package com.rl.dao; import java.util.List; ...

  4. Sublime Text 3 使用MarkDown编写带预览的文本

    看到别人使用一个叫Markdown的标记语言来完成编码,心里就有点小激动,毕竟简短的几个符号,就可以写出如此精美的界面,实在是让人感到心旷神怡啊.于是我就在网上搜索了一些相关项的设置,于是便有了下面的 ...

  5. CMake设置FOLDER失败及解决

    CMake可以设置FOLDER属性,用来分目录组织VC中的多个工程. FOLDER: Set the folder name. Use to organize targets in an IDE. T ...

  6. java 判断是否是周末

    package untitled7; import java.util.Date; import java.text.SimpleDateFormat; import java.util.Calend ...

  7. Spark SQL官方文档阅读--待完善

    1,DataFrame是一个将数据格式化为列形式的分布式容器,类似于一个关系型数据库表. 编程入口:SQLContext 2,SQLContext由SparkContext对象创建 也可创建一个功能更 ...

  8. STL:vector容器用法详解

    vector类称作向量类,它实现了动态数组,用于元素数量变化的对象数组.像数组一样,vector类也用从0开始的下标表示元素的位置:但和数组不同的是,当vector对象创建后,数组的元素个数会随着ve ...

  9. 高通 8x12 添加 TP和按键

    1 .在tp的驱动文件中添加以下代码实现按键功能 [plain] view plain copy static ssize_t ft5x06_virtual_keys_register(struct  ...

  10. (四十七)Quartz2D引擎初步

    Quartz2D是跨平台的,同时支持iOS与Mac. 支持圆型裁剪,可以实现圆形头像等功能,也支持手势解锁.折线图等的制作. 对于复杂的UI界面,还可以通过Quartz2D将控件内部的结构画出来,可用 ...