【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:

begin to intersect at node c1.

Example 1:

Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
Output: Reference of the node with value = 8
Input Explanation: The intersected node‘s value is 8 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,0,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.

Example 2:

Input: intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
Output: Reference of the node with value = 2
Input Explanation: The intersected node‘s value is 2 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [0,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.

Example 3:

Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
Output: null
Input Explanation: From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.
Explanation: The two lists do not intersect, so return null.

思路

  这道题意思就是让我们求两个链表第一个公共节点,第一种办法就是暴力破解法,我们先将一个链表的节点都存进辅助列表中。然后遍历第二个链表,每一次都判断是否在辅助列表中,当遍历完毕之后没找到说明不存在入口节点,存在则直接返回。但是这种算法的时间复杂度较高O(m*n),当列表数目过多时,会存在超时的问题。还有一种办法就是如果两个链表存在入口时,入口节点后面的元素数目都是相同的,根据这一个规则我们可以设置设置两个指针分别指向两个链表的头节点,然后开始遍历。当第一个指针指向链表A遍历完毕之后,将其指向链表B的头部。第二个指针指向链表B遍历完毕之后将其指向链表A,然后循环遍历,当两个节点相同时终止遍历,如果存在入口节点则终止时则会指向入口节点,否则指向None。解决代码

  第一种解决代码
 1 # Definition for singly-linked list.
 2 # class ListNode(object):
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.next = None
 6
 7 class Solution(object):
 8     def getIntersectionNode(self, headA, headB):
 9         """
10         :type head1, head1: ListNode
11         :rtype: ListNode
12         """
13         if not headA or not headB:
14             return None
15         tem_list = []
16         while headA:         # 先将链表A中的节点进行存储
17             tem_list.append(headA)
18             headA = headA.next
19         while headB:      # 然后遍历链表B进行节点的查找
20             if headB in tem_list:
21                 return headB
22             headB = headB.next
23         return None
第二种解决代码
 1 # Definition for singly-linked list.
 2 # class ListNode(object):
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.next = None
 6
 7 class Solution(object):
 8     def getIntersectionNode(self, headA, headB):
 9         """
10         :type head1, head1: ListNode
11         :rtype: ListNode
12         """
13         pa, pb = headA, headB          # 两个指针指向两个链表
14         while pa is not pb:           # pa不等于pb,
15             pa = pa.next if pa else headB        # 遍历到尾部时,pa指向headB头部
16             pb = pb.next if pb else headA          # 遍历到尾部时,pb指向headA的头部
17         return pa            

原文地址:https://www.cnblogs.com/GoodRnne/p/10989096.html

时间: 2024-10-16 05:51:54

【LeetCode每天一题】 Intersection of Two Linked Lists(两个链表的入口节点)的相关文章

lintcode 中等题:Intersection of Two Linked Lists 两个链表的交叉

题目 两个链表的交叉 请写一个程序,找到两个单链表最开始的交叉节点. 样例 下列两个链表: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 在节点 c1 开始交叉. 注意 如果两个链表没有交叉,返回null. 在返回结果后,两个链表仍须保持原有的结构. 可假定整个链表结构中没有循环. 挑战 需满足 O(n) 时间复杂度,且仅用 O(1) 内存. 解题 尝试用时间复杂度是O(NM),却没有解决,在这个博客看到根据两个链表的特性进行解决. 就如同上图,两个链表相交的部

[LC]141题 Intersection of Two Linked Lists (相交链表)(链表)

①中文题目 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 注意: 如果两个链表没有交点,返回 null.在返回结果后,两个链表仍须保持原有的结构.可假定整个链表结构中没有循环.程序尽量满足 O(n) 时间复杂度,且仅用 O(1) 内存. ②思路 遍历,O(M*N)的遍历,直到找出相等的结点(不是val相等) ③我的代码(很慢) 1 public class Solution { 2 public ListNode getIntersectionNod

leetcode_160题——Intersection of Two Linked Lists(线性表,哈希表)

Intersection of Two Linked Lists Total Accepted: 28581 Total Submissions: 100989My Submissions Question Solution Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists

[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]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: begin to intersect at node c1. Example 1: Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5],

LeetCode【160】Intersection of Two Linked Lists

For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. 思路比较清晰,首先确定二者的长度并计算长度之差的绝对值dis,让较长链表先走dis步,然后两个指针一起走.如果两个指针相等,则返回,否则当指针达到NULL时,没有交点. AC代码如下: void getLength(ListNode *head , int& len)

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] 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

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