题目简述:

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.

解题思路:

首先这道题目有他的特殊性,这个特殊性就在这里的两个链表如果有交集的话,那么他们在最后面的一定都是相同的。

所以这里催生了两种想法:

  1. 从后往前比较,找到最后形同的那个

  2. 从前往后比较,但是这里要用到一个技巧。我们首先要去获取到这两个链表的长度,然后让长度长的那个先多走长度差的距离,最后再开始比较,第一个相同的即是。

    这里使用了第二种思路:

    Definition for singly-linked list.

    class ListNode:

    def init(self, x):

    self.val = x

    self.next = None

    class Solution:

    @param two ListNodes

    @return the intersected ListNode

    def getIntersectionNode(self, headA, headB):

    ta = ListNode(0)

    tb = ListNode(0)

    ta = headA

    tb = headB

    la = 0

    lb = 0

    while ta != None:

    la += 1

    ta = ta.next

    while tb != None:

    lb += 1

    tb = tb.next

    if la > lb :

    ta = headA

    tb = headB

    tt = la - lb

    while tt > 0:

    ta = ta.next

    tt -= 1

    while ta != None and tb != None:

    if ta == tb:

    return ta

    ta = ta.next

    tb = tb.next

    return None

         else:
    ta = headA
    tb = headB
    tt = lb -la
    while tt > 0:
    tb = tb.next
    tt -= 1
    while ta != None and tb != None:
    if ta.val == tb.val:
    return ta
    ta = ta.next
    tb = tb.next
    return None

【leetcode】Intersection of Two Linked Lists的更多相关文章

  1. 【leetcode】Intersection of Two Linked Lists(easy)

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

  2. 【LeetCode】Intersection of Two Linked Lists(相交链表)

    这道题是LeetCode里的第160道题. 题目讲的: 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, ...

  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(2个链表的公共节点)

    Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...

  5. 【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 ...

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

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

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

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

随机推荐

  1. 使用vlc播放器播放rtsp流视频

    可参考: 使用vlc播放器做rtsp服务器 web网页中使用vlc插件播放相机rtsp流视频 使用vlc进行二次开发做自己的播放器 首先需要安装vlc播放器,下载及安装步骤略 使用vlc播放器播放rt ...

  2. Spring,Mybatis 整合Memcache

    Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度.Memcached ...

  3. oracle服务起不来以及无法监听问题解决

    改问题是在搭建起一个很早之前的数据库的时候碰见的,虽然这个问题网上已经有很多相关的帖子,但因最近碰见多次这样的问题,特此简单记录: 1.最开始碰见的问题是:The listener supports ...

  4. leetcode--Maximum Subarray

    题目链接:https://leetcode.com/problems/maximum-subarray/ 算法类型:动态规划 题目分析:最大序列和 代码实现: class Solution(objec ...

  5. document.compatMode属性

    document.compatMode用来判断当前浏览器采用的渲染方式. 官方解释: BackCompat:标准兼容模式关闭.CSS1Compat:标准兼容模式开启. 当document.compat ...

  6. Create a new Windows service on windows server2012

    netsh http add iplisten ipaddress=0.0.0.0:15901 sc.exe create "FPPService" binPath= " ...

  7. mysql 中基础英语单词 (一)关于数据库创建与查找 (包括简写单词)

    create 创建             limit 限制        count  计算     rollup  几上归纳 drop   降下,撤销                       ...

  8. 【Android接百度地图API】百度地图Demo点击按钮闪退

    运行百度地图自带的BaiduMap_AndroidSDK_v4.1.0_Sample里面的BaiduMapsApiASDemo发现点击上面的按钮会闪退,控制台报的是xml的问题 查了一下,官方文档特别 ...

  9. C和指针 第十七章 经典数据类型 堆栈 队列 二叉树

    堆栈: // // Created by mao on 16-9-16. // #ifndef UNTITLED_STACK_H #define UNTITLED_STACK_H #define TR ...

  10. Filter实现用户名验证

    ①:使用Filter,判断用户名是否为空,为空的话返回登录画面. 1,web.xml: 1.<filter> 2. <filter-name>SecurityServlet&l ...