题目

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.

代码:oj在线测试通过 Runtime: 1604 ms

 # 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 getListLen(self,head):
length = 0
while head is not None:
length += 1
head = head.next
return length def getIntersectionNode(self, headA, headB):
if headA is None or headB is None:
return None hA = headA
hB = headB lenA = self.getListLen(hA)
lenB = self.getListLen(hB) if lenA > lenB :
distance = lenA - lenB
for i in range(0,distance):
hA = hA.next
if lenA < lenB :
distance = lenB -lenA
for i in range(0,distance):
hB = hB.next intersection = None
while hA is not None and hB is not None:
if hA == hB:
return hA
else:
hA = hA.next
hB = hB.next
return intersection

思路

1. 首先要记录两个Linked List的长度

2. 双指针 分别指向两个List 指向长List的先走若干步,获得一个新的Linked List表头

3. 逐一比较两个List的每个指针的值是否相等:直到找到相等的,或者到None

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

  1. LeetCode: Intersection of Two Linked Lists 解题报告

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

  2. [LeetCode] Intersection of Two Linked Lists 求两个链表的交点

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

  3. LeetCode Intersection of Two Linked Lists

    原题链接在这里:https://leetcode.com/problems/intersection-of-two-linked-lists/ 思路:1. 找到距离各自tail 相同距离的起始List ...

  4. LeetCode——Intersection of Two Linked Lists

    Description: Write a program to find the node at which the intersection of two singly linked lists b ...

  5. [LeetCode] 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 Intersection of Two Linked Lists (找交叉点)

    题意: 给两个链表,他们的后部分可能有公共点.请返回第一个公共节点的地址?若不重叠就返回null. 思路: 用时间O(n)和空间O(1)的做法.此题数据弱有些弱. 方法(1)假设两个链表A和B,用两个 ...

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

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

  9. [LintCode] Intersection of Two Linked Lists 求两个链表的交点

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

  10. 2016.5.24——Intersection of Two Linked Lists

    Intersection of Two Linked Lists 本题收获: 1.链表的输入输出 2.交叉链表:这个链表可以有交叉点,只要前一个节点的的->next相同即可. 题目:Inters ...

随机推荐

  1. sublime打开txt文件乱码的问题

    我们使用Sublime打开TXT文件的时候,会经常因为编码的问题造成乱码. 这是因为TXT记事本的默认保存编码格式是GBK,而Sublime text不支持GB2312和GBK编码. 我们可以通过安装 ...

  2. centos 7 iptables基本配置

    安装iptable iptable-service #先检查是否安装了iptables service iptables status #安装iptables yum install -y iptab ...

  3. jQuery 效率提升建议

    jQuery简洁通用的方法集把编码者从繁重的工作中解脱出来,也拉低了进入javascript的门槛,初学者对浏览器兼容性一无所知的情况下,几行代码就可以写出超炫的特效.网上有一篇文章转载比较泛滥,已经 ...

  4. Centos 5.2下安装多个mysql数据库

    一.编译安装第一个MySQL 5.1.33 cd /opt/usr/sbin/groupadd mysql/usr/sbin/useradd -g mysql mysql -s /bin/nologi ...

  5. python3 安装turtle tkitnter 报错

    导入tkinter模块后,运行出现No module named _tkinter, please install the python-tk package ubuntu运行tkinter错误   ...

  6. acc_set_device_num && acc_get_device_num例程

    void acc_set_device_num( int, acc_device_t ); 描述在指定类型的所有设备中, acc_set_device_num 告诉运行时库环境用哪一个设备.如果 de ...

  7. Windows核心编程-作业

    原文链接:http://zhujiangtao.com/?p=983 作业 作业 一个简单例程 CreateJobObject 创建作业 作业限制和 SetInformationJobObject A ...

  8. java设计模式——桥接模式

    一. 定义与类型 定义:将抽象部分与他的具体实现部分分离,使它们都可以独立的变化,通过组合的方式建立两个类之间的联系,而不是继承 类型:结构性. 二. 使用场景 (1) 抽象和具体实现之间增加更多的灵 ...

  9. 第28章 LTDC—液晶显示中英文—零死角玩转STM32-F429系列

    第28章     LTDC—液晶显示中英文 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/f ...

  10. webkit几种内核版本的优劣对比总结

    01.Open-Webkit-Sharp 默认不存在JS对话框,如果需要显示alert box,则需要在ShowJavaScriptAlertPanel进行捕捉,然后显示对话框: 02.Open-We ...