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

例如,下面的两个链表:

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. 【紫书】Ordering Tasks UVA - 10305 拓扑排序:dfs到底再输出。

    题意:给你一些任务1~n,给你m个数对(u,v)代表做完u才能做v 让你给出一个做完这些任务的合理顺序. 题解:拓扑排序版题 dfs到底再压入栈. #define _CRT_SECURE_NO_WAR ...

  2. iOS 问答时间

    runloop 的 model作用是什么? 答案: model 主要是用来指定事件在运行循环中的优先级,分为: NSDefaultRunLoopMode(kCFRunLoopDefaultMode): ...

  3. LeetCode 852 Peak Index in a Mountain Array 解题报告

    题目要求 Let's call an array A a mountain if the following properties hold: A.length >= 3 There exist ...

  4. Chrome中安装Firebug插件

    Chrome中的Firebug插件:Firebug Lite 1.Firebug Lite下载:http://chromecj.com/web-development/2015-05/471/down ...

  5. Python pip 如何升级

    场景:部署环境时,在线安装第三方库(pip install flask-bootstrap),提示pip版本过低. 解决方法一:        命令: python -m pip install -- ...

  6. vue中watch的详细用法

    在vue中,使用watch来响应数据的变化.watch的用法大致有三种.下面代码是watch的一种简单的用法: <input type="text" v-model=&quo ...

  7. MySQL中死锁(转)

    add by zhj: 总结一下,MySQL有主动和被动两种方式检测死锁. 主动方式:检查锁等待的图,如果有环,那就有死锁,这种情况下,会回滚事务. 被动方式:等待锁超时(即innodb_lock_w ...

  8. java的错题整理

    为了阅读方便,我们写代码时要缩进,以便于更好的理解代码 对象是是具有相同属性和共同行为的一组类的实例,不是集合. B是标准格式,D没有对象接收它,所以这样子. boolean的默认值是false如果一 ...

  9. ThreadLocal(一):Thread 、ThreadLocal、ThreadLocalMap

    一.ThreadLocalMap是ThreadLocal的内部类.Thread持有ThreadLocalMap的引用 Entry类继承了WeakReference<ThreadLocal< ...

  10. 10.8-uC/OS-III内部任务(中断处理任务 OS_IntQTask())

    1.当设置OS_CFG.H中的OS_CFG_ISR_POST_DEFERRED_EN为1时, uC/OS-III就会创建一个任务,它的作用是尽快完成ISR中对post函数的调用, 将信号量.消息等对象 ...