(LeetCode)Intersection of Two Linked Lists --- 两个链表的交集
来源:互联网 发布:软件市场营销方案 编辑:程序博客网 时间:2024/06/06 05:10
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.
Credits:
Special thanks to @stellari for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
解题分析:
对于这个题目来说,我的做法就是先判断出哪个LinkedList 比较小。
然后可循环比较最小的次数,来达到最高的效率,没有环,更好判断一些。
加上条件判断,就可以判断出来是否末尾的串有交集。
# -*- coding:utf-8 -*-__author__ = 'jiuzhang'class ListNode(object): def __init__(self, x): self.val = x self.next = Noneclass Solution(object): def getIntersectionNode(self, headA, headB): """ :type head1, head1: ListNode :rtype: ListNode """ listA= [] listB= [] if headA == None or headB == None: return None while 1: if headA == None: break listA.append(headA.val) headA = headA.next while 1: if headB == None: break listB.append(headB.val) headB = headB.next if listA[-1] != listB[-1]: return None if len(listA) < len(listB): minLen = len(listA) else: minLen = len(listB) for i in range(1, minLen + 1): if listA[-i] != listB[-i]: return ListNode(listA[-i + 1]) if i == minLen: return ListNode(listA[-i])
0 0
- 【leetcode 两个链表的交集点】Intersection of Two Linked Lists
- 【leetcode 两个链表的交集点】Intersection of Two Linked Lists
- (LeetCode)Intersection of Two Linked Lists --- 两个链表的交集
- 160. Intersection of Two Linked Lists (求两个链表的交集)
- LeetCode OJ 之 Intersection of Two Linked Lists (两个链表的交点)
- [leetcode]Intersection of Two Linked Lists[java]找出两个链表的公共节点
- LeetCode--Intersection of Two Linked Lists (两个链表的交点)Python
- leetcode_160. Intersection of Two Linked Lists 找两个单链表的交集的起始位置
- 【LeetCode-面试算法经典-Java实现】【160-Intersection of Two Linked Lists(单链表的交集)】
- LeetCode---(160)Intersection of Two Linked Lists判断两个链表是否相交
- LeetCode—Intersection of Two Linked Lists 找两个链表的相交位置,让长的链表先走一段
- leetcode解题之160# Intersection of Two Linked Lists Java版 (两个链表的第一个公共结点)
- [Lintcode]Intersection of Two Linked Lists 两个链表的交叉
- 380.Intersection of Two Linked Lists-两个链表的交叉(中等题)
- 160 Intersection of Two Linked Lists 求两个链表的交点
- [leetcode] [leetcode]Intersection of Two Linked Lists
- 【LeetCode】Intersection of Two Linked Lists
- LeetCode: Intersection of Two Linked Lists
- Java 高并发相关--多线程基础概念
- maven的pom介绍及配置
- SharedPreferences存储数据之用户名实例
- 替换
- R时间序列举例(预测未来几个月的用气量)
- (LeetCode)Intersection of Two Linked Lists --- 两个链表的交集
- mysql left on 和where中执行顺序
- Java 7之基础 - 强引用、弱引用、软引用、虚引用
- 为MySQL数据库建立索引
- 闭包学习
- 【Android】50、碎片的简单用法
- hdu2256Problem of Precision+矩阵快速幂+广义斐波拉契
- C++实现链表
- hadoop启动错误:Path /XXX should be specified as a URI in configuration file