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

好久没有耍代码了,手有点生,还有点痒,and 脑子有点锈。话说这个简单的东西,耍了我一个小时。

主要是最近很少碰代码。二是对python不熟悉,刚刚接触,但是顿时很热爱这个小蟒蛇。于是打算好好蹂躏它。

题目链接:https://oj.leetcode.com/problems/intersection-of-two-linked-lists/

我的思路是这样的。

首先没有循环,所以不用判断是否绕一圈回来了。其次,看题意不会有多次分支,即分支只有一个,且一直相同到尾部。那么好办了。

我只需要逆向判断字符串是否相等就好了。

最后一个字符不相等。直接pass;反之,一直比较到看到不同的字符为止,输出就好了。

以上是我在实现之前的简单想法,貌似可行。但是实际实现的时候,发现很多细节问题遗漏了。这也是我很久没有摸代码的一个后果。

比如,碰到输入链表是空的,要处理;碰到一个字符串是另一个的子集,即在完成比较之前,就退出循环了,要处理;还有几个循环边界的问题。

所以说,感觉还是多码豆豆有好处。码豆豆给了我深度思考的习惯。

ok,上代码:

Runtime: 1576
ms                              42 / 42 test
cases passed.

# 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):
        listA = []
        listB = []
        if headA == None or headB == None:# 判断输入表是否为空
            return None

        while 1:#链表赋值到list中,方便从后面比较
            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]:# list最后一个元素不等
            return None;
        if len(listA)<len(listB):# 以最短链表长度为准比较
            minLen = len(listA)
        else:
            minLen = len(listB)

        inster = []
        for i in range(1,minLen+1) # 边界问题,因为是逆向获取数据,因此,比较到第一个元素的时候。 listA[0] = listA[-len]
            if listA[-i] != listB[-i]:
                return ListNode(listA[-i+1])
            if i== maxLen:    #处理两个表存在子集的情况
                return ListNode(listA[-i])

【总结一下】python中,list元素会出现index out of length的情况。python的基础不熟悉,产生了低级错误,应当多注意对list内容的审核,过滤。另,写的python程序总是不简洁。始终有c的影子在里面。  得多写写。

另外,作者的solution是这样的,提供了三个方法:

1、暴力搜索:对headA中每个元素,分别比较headB中每个元素。 0(mn)

2、hash表方法:对每个元素的address和值建立hash,第一个相等的,获取address。 python里面,可以用字典了。

3、两个指针的方法:这个方法有人实现了。 因为这个题目的特殊性,通过让较长的链表多走比较短链表长的步数,再同时移动两个链表的指针进行比较搜索的方法。实现的算法。总之都是简答题,权当熟悉语法。

keep on coding!

时间: 2024-11-05 22:53:33

[leetcode]Intersection of Two Linked Lists —— 熟悉python的相关文章

leetcode Intersection of Two Linked Lists python

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: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. python

leetcode 【 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: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. 代码:oj在线测试通过 Runtime: 1604 ms 1 # Def

[LeetCode] Intersection of Two Linked Lists

Question: 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. 1.题型分类: 2.思路: 3.时间复杂度:O(n) 4.代

LeetCode——Intersection of Two Linked Lists

Description: 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. 求两个链表的相交的部分 /** * Definitio

[LeetCode] 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: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have no i

LeetCode—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: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have no i

[LeetCode] 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: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have no i

LeetCode Intersection of Two Linked Lists 解题报告

https://oj.leetcode.com/problems/intersection-of-two-linked-lists/ 求两个链表的第一个公共节点,如果不存在公共节点的话就返回null. A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 解题思路: 1)如果两个链表的最后一个节点一样,那么说明两个链表一定有交点. 2)分别求出两个链表的长度,然后对长度长的链表向前移动:LengA - LengB,将两个链表进行对齐,之后一起遍历,直到找到第一个相同的节

LeetCode Intersection of Two Linked Lists (找交叉点)

题意:给两个链表,他们可能后部分有重叠的地方(重叠是指,两链表都有元素处于同一块地址上),一旦重叠到就不可能分开啦.问在哪重叠的?当然是返回地址了.不重叠就返回null. 思路:用O(n)和O(1)的做法. (1)若两串会重叠,那么到最后一个元素必定会相遇. (2)先判断是否有重叠. (3)有重叠了,要考虑两串长度不一样.后段肯定一样,不重叠的那一段可能不一样.要让它们同长才行,使得两个指针每次只走1步才会相遇. (4)交叉遍历一次就可以使得两个指针处于那样的位置啦.一个指向短链的开头,一个指向