听歌曲初爱有感:

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

题目:

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. JRE System Library [JavaSE-1.7](unbound)

    window > preferences > java > Install jars >如果没有jdk1.7 ,点击下面的search,会自动找到已经安装对jdk1.7,选择, ...

  2. iOS中的两种搜索方式UISearchDisplayController和UISearchController

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 以前iOS的搜索一般都使用UISearchDisplayCon ...

  3. 头部——MimeHeaders

    http协议的请求头部更像一个键值对,例如Content-Length : 123,前面为键后面为值,表示文本长度为123.对于若干个头部在请求对象中被封装成MimeHeaders对象,MimeHea ...

  4. 使用C++的string实现高精度加法运算

    对于超大数字的运算,用long long int仍然不能解决,这时候就需要考虑通过模拟运算和数组存储来实现高精度运算. 本文讨论借助C++的string来实现高精度的运算. 首先输入的量直接存储为st ...

  5. pandas小记:pandas基本设置

    http://blog.csdn.net/pipisorry/article/details/49519545 ): print(df) Note: 试了好久终于找到了这种设置方法! 它是这样实现的 ...

  6. Android使用SimpleAdapter

    SimpleAdapter的使用步骤如下: 声明ListView,并进行初始化操作 准备数据集,一般用list来实现,当然也可以使用数组 为listview适配simpleadapter 如下代码: ...

  7. C++ Primer 有感(标准库map类型)

    map是键-值对的集合.map类型通常可以理解为关联数组:可以使用键作为下标获取一个值,正如内置数组一样.而关联的本质在于元素的值于某个特定的键相关联,而并非通过元素在数组中的位置获取. 1.map对 ...

  8. Mac 下 Chrome多个Tab之间切换

    下一个Tab: Control + Tab前一个Tab: Control + Shift + Tab记录一下备忘.

  9. C语言--字符串和数字的相互转换

    1.数字转换为字符串 sprintf 跟printf 在用法上几乎一样,只是打印的目的地不同而已,前者打印到字符串中,后者则直接在命令行上输出. sprintf 是个变参函数,定义如下: int sp ...

  10. Android指南针之加速度传感器地磁传感器-android学习之旅(67)

    由于andorid不推荐用传统的方向传感器,推荐用加速度传感器和地磁传感器来构造得到方向传感器的数据,其实主要是z轴的旋转角度 具体代码示例 代码如下 public class MainActivit ...