编写一个程序,找到两个单链表相交的起始节点。

例如,下面的两个链表:

A:          a1 → a2

c1 → c2 → c3

B: b1 → b2 → b3

在节点 c1 开始相交。

注意:

  • 如果两个链表没有交点,返回 null.
  • 在返回结果后,两个链表仍须保持原有的结构。
  • 可假定整个链表结构中没有循环。
  • 程序尽量满足 O(n) 时间复杂度,且仅用 O(1) 内存。
    /**
    * 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) {
    if (headA == null || headB == null) {
    return null;
    }
    int lenA = len(headA);
    int lenB = len(headB);
    if (lenA>lenB) {
    while (lenA != lenB) {
    headA = headA.next;
    lenA--;
    }
    } else {
    while (lenA != lenB) {
    headB = headB.next;
    lenB--;
    }
    }
    while (headA != headB) {
    headA = headA.next;
    headB = headB.next;
    } return headA;
    }
    private int len(ListNode headA) {
    int len = 0;
    while (headA != null) {
    headA = headA.next;
    len++;
    }
    return len;
    }
    }

LeetCode160.相交链表的更多相关文章

  1. [Swift]LeetCode160. 相交链表 | 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. LeetCode160 相交链表(双指针)

    题目: click here!!题目传送门 思路: 1.笨方法 因为如果两个链表相交的话,从相交的地方往后是同一条链表,所以: 分别遍历两个链表,得出两个链表的长度,两个长度做差得到n,然后将长的链表 ...

  3. 【LeetCode题解】160_相交链表

    目录 160_相交链表 描述 解法一:哈希表 思路 Java 实现 Python 实现 解法二:双指针(推荐) 思路 Java 实现 Python 实现 160_相交链表 描述 编写一个程序,找到两个 ...

  4. LeetCode 160: 相交链表 Intersection of Two Linked Lists

    爱写Bug(ID:iCodeBugs) 编写一个程序,找到两个单链表相交的起始节点. Write a program to find the node at which the intersectio ...

  5. LeetCode 160——相交链表(JAVA)

    编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4,5], listB ...

  6. leetcode腾讯精选练习之相交链表(六)

    相交链表 题目: 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4,5 ...

  7. Java实现 LeetCode 160 相交链表

    160. 相交链表 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4, ...

  8. 【原创】leetCodeOj --- Intersection of Two Linked Lists 解题报告(经典的相交链表找交点)

    题目地址: https://oj.leetcode.com/problems/intersection-of-two-linked-lists/ 题目内容: Write a program to fi ...

  9. LeetCode 160 相交链表

    题目: 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4,5], li ...

随机推荐

  1. Django:模板template(二)

    将跨站请求伪造和验证码的东西记一下 CSRF Cross Site Request Forgery.跨站请求伪造 链接:GET请求:表单:POST请求 某些恶意的网站上,包含链接.表单.按钮.Java ...

  2. 量子杨-Baxter方程新解系的一般量子偶构造_爱学术 https://www.ixueshu.com/document/f3385115a33571aa318947a18e7f9386.html

    量子杨-Baxter方程新解系的一般量子偶构造_爱学术 https://www.ixueshu.com/document/f3385115a33571aa318947a18e7f9386.html

  3. [ovs][dpdk] ovs-dpdk 线程数,收包队列,core绑定

    http://docs.openvswitch.org/en/latest/intro/install/dpdk/?highlight=dpdk 绑定2,4,6, 8核 [root@vrouter1 ...

  4. python编码类型互转总结

    1.只有在unicode下才能将utf-8与gbk互转2.unicode是在内存中使用,bytes是文件存储和网络传输时使用-------------------------------------- ...

  5. Java如何编写Servlet程序

    一:Servlet Servlet是Java服务器端编程,不同于一般的Java应用程序,Servlet程序是运行在服务器上的,服务器有很多种,Tomcat只是其中一种. 例子: 在Eclipse中新建 ...

  6. 浅谈Trie树

    Trie树,也叫字典树.顾名思义,它就是一个字典 字典是干什么的?查找单词!(英文字典哦) 个人认为字典树这个名字起得特别好,因为它真的跟字典特别像,一会r你就知道了. 注:trie的中文翻译就是单词 ...

  7. NOIP国王游戏

    #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...

  8. c语言递归函数的调用

    int fun(); int main() { int n,sum=0,i; scanf("%d",&n); for (i=1; i<=n; i++) { sum+= ...

  9. 【JMeter】基础元件

    测试计划(Test Plan) 它用来描述一个测试方案,包含与本次性能测试所有相关的功能.也就说本次测试的所有内容是于基于一个计划的. “函数测试模式”复选框,如果被选择,它会使Jmeter记录来自服 ...

  10. (4.26)sql server存储过程优化

    此博客介绍了简单但有用的提示和优化,以提高存储过程的性能. 0.with recompile:重编译 exec uspGetSalesInfoForDateRange ‘1/1/2009’, 31/1 ...