关闭

(LeetCode)Intersection of Two Linked Lists --- 两个链表的交集

625人阅读 评论(0) 收藏 举报
分类:

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 = None

class 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上最新的面试题目,求两个链表的交集,难度为easy,附个人理解解析以及题目作者的英文解析。
  • u012162613
  • u012162613
  • 2014-11-28 00:15
  • 8383

LeetCode 160 Intersection of Two Linked Lists(链表相交)(Linked List)(*)

翻译写一个程序来找出两个单链表相交的开始处。例如,如下有两个链表:在节点c1处开始相交。批注: 如果两个链表根本没有相交,返回NULL。 在函数返回后链表必须保留原有的数据结构。 你可以假设在整个链结构中没有循环。 你的代码最后可以在O(n)时间和O(1)空间运行。原文Write a program...
  • NoMasp
  • NoMasp
  • 2016-01-24 09:26
  • 5074

LeetCode 160 — Intersection of Two Linked Lists(C++ Python)

题目:https://oj.leetcode.com/problems/intersection-of-two-linked-lists/ Write a program to find the node at which the intersection of two singly linked...
  • dragon_dream
  • dragon_dream
  • 2014-12-02 20:59
  • 1851

leetcode 160: Intersection of Two Linked Lists

Intersection of Two Linked Lists Total Accepted: 8676 Total Submissions: 32571 Write a program to find the node at which the intersection of two si...
  • xudli
  • xudli
  • 2014-12-30 03:17
  • 2755

【LeetCode-面试算法经典-Java实现】【160-Intersection of Two Linked Lists(单链表的交集)】

【160-Intersection of Two Linked Lists(单链表的交集)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Write a program to find the node at which the intersection of two ...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-22 06:45
  • 3379

160. Intersection of Two Linked Lists(python)

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: ...
  • qq_28119401
  • qq_28119401
  • 2016-09-18 21:38
  • 403

[leetcode]Intersection of Two Linked Lists —— 熟悉python

好久没有耍代码了,手有点生,还有点痒,and 脑子有点锈。话说这个简单的东西,耍了我一个小时。 主要是最近很少碰代码。二是对python不熟悉,刚刚接触,但是顿时很热爱这个小蟒蛇。于是打算好好蹂躏它。 题目链接:https://oj.leetcode.com/problems/intersectio...
  • ls1160
  • ls1160
  • 2014-12-11 11:41
  • 1601

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 example, the following two linked lists:  A: ...
  • sunao2002002
  • sunao2002002
  • 2015-05-14 22:06
  • 1302

LeetCode Add Two Numbers两个链表数值相加

Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes ...
  • kenden23
  • kenden23
  • 2013-11-20 14:01
  • 1680

leetcode_350 Intersection of Two Arrays II

题目分析:给定两个数组,求数组的交集。输出结果中的元素不唯一,输出数组可以无序。 解题思路:1)对数组nums1进行排序;2)对数组nums2进行排序;3)遍历数组nums1和nums2中元素,并比较对应的元素,若相等,则将其保存到输出结果中,并变化两个数组对应的索引 不等,则变化较小元素对应的索引...
  • yzhang6_10
  • yzhang6_10
  • 2016-05-28 18:17
  • 2210
    个人资料
    • 访问:1594036次
    • 积分:29109
    • 等级:
    • 排名:第212名
    • 原创:1387篇
    • 转载:21篇
    • 译文:8篇
    • 评论:220条
    Social NetWork
    博客专栏
    最新评论