听歌曲初爱有感:

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

题目:

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. JVM的GC(概念与深入)

    深入浅出了解什么是GC: http://my.oschina.net/xianggao/blog/86985 GC策略详解: http://blog.csdn.net/winniepu/article ...

  2. UNIX网络编程——使用线程的TCP回射服务器程序

    同一进程内的所有线程除了共享全局变量外还共享: (1)进程指令: (2)大多数数据: (3)  打开的文件(即描述符): (4)信号处理函数和信号处置: (5)当前工作目录: (6)用户ID和组ID. ...

  3. 查看LOV对应查询语句的研究

    一.获取当前会话id 1.方法一 tools: Help > About 2.方法二 打开个性化定义界面(如果没有权限,到系统配置文件设置中,查看是否是"隐藏诊断菜单"被设置 ...

  4. 可视化分析工具Cytoscape使用记录

    最近项目要使用到可视化分析工具Cytoscape,所以会花费很多的时间跟精力来整理Cytoscape软件使用和开发的相关资料,希望写下的文章能减少有兴趣的同行学习跟开发所走的弯路时间.同时也是因为百度 ...

  5. 【一天一道LeetCode】#102. Binary Tree Level Order Traversal

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

  6. Spark-streaming 连接flume

    1,程序为spark的example中的FlumeEventCount示例 object FlumeEventCount { def main(args: Array[String]) { Strea ...

  7. Jeff Atwood质疑iPhone的单键设计

    我喜欢使用iPhone,但我对它的一个设计不敢苟同:苹果始终坚持,设备的正面永远只能有一个按键. 我还买了一个Kindle Fire,它更离谱,一个按键都没有!我完全赞成,任何小器具的正面都应该在明显 ...

  8. javascript之自定义数组工具对象

    <pre name="code" class="html">/* 需求:编写一个js文件,在js文件中自定义一个数组工具对象, 该工具对象要有一个找 ...

  9. android官方技术文档翻译——Case 标签中的常量字段

    本文译自androd官方技术文档<Non-constant Fields in Case Labels>,原文地址:http://tools.android.com/tips/non-co ...

  10. naoting

    生活就像一锅菠菜汤 20160714 夜