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 # Definition for singly-linked list.
 2 # class ListNode:
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.next = None
 6
 7 class Solution:
 8     # @param two ListNodes
 9     # @return the intersected ListNode
10     def getListLen(self,head):
11         length = 0
12         while head is not None:
13             length += 1
14             head = head.next
15         return length
16
17     def getIntersectionNode(self, headA, headB):
18         if headA is None or headB is None:
19             return None
20
21         hA = headA
22         hB = headB
23
24         lenA = self.getListLen(hA)
25         lenB = self.getListLen(hB)
26
27         if lenA > lenB :
28             distance = lenA - lenB
29             for i in range(0,distance):
30                 hA = hA.next
31         if lenA < lenB :
32             distance = lenB -lenA
33             for i in range(0,distance):
34                 hB = hB.next
35
36         intersection = None
37         while hA is not None and hB is not None:
38             if hA == hB:
39                 return hA
40             else:
41                 hA = hA.next
42                 hB = hB.next
43         return intersection

思路

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

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

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

时间: 2024-10-12 18:24:01

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

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 —— 熟悉python

好久没有耍代码了,手有点生,还有点痒,and 脑子有点锈.话说这个简单的东西,耍了我一个小时. 主要是最近很少碰代码.二是对python不熟悉,刚刚接触,但是顿时很热爱这个小蟒蛇.于是打算好好蹂躏它. 题目链接:https://oj.leetcode.com/problems/intersection-of-two-linked-lists/ 我的思路是这样的. 首先没有循环,所以不用判断是否绕一圈回来了.其次,看题意不会有多次分支,即分支只有一个,且一直相同到尾部.那么好办了. 我只需要逆向判

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)交叉遍历一次就可以使得两个指针处于那样的位置啦.一个指向短链的开头,一个指向

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