zoukankan      html  css  js  c++  java
  • *160. Intersection of Two Linked Lists

    1. 原始题目

    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.
    

    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.

    2. 题目理解

    就是找两个链表的交叉结点。注意:空链表;不存在交叉点的链表

    3. 解题

    思路:用两个栈:依次将链表a和b分别进栈。然后出栈时比较两个元素是否相等。如果遇到不同的结点,那么该结点的上一个结点就是交叉节点。

     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 headA is None or headB is None:     # 空链表处理
    14             return None
    15         results = None
    16         stacka = []
    17         stackb = []
    18         a = headA
    19         b = headB
    20         while(a):                              # a进栈  
    21             stacka.append(a)
    22             a = a.next
    23         while(b):                              # b进栈  
    24             stackb.append(b)
    25             b = b.next
    26         
    27         i = -1
    28         if stacka[i]!=stackb[i]:               # 头元素都不相等肯定没有交叉点
    29             return None
    30         while (stacka[i]==stackb[i]):
    31                 i = i-1
    32                 if abs(i)>len(stacka) or abs(i)>len(stackb):
    33                     break
    34         results = stacka[i+1]                  # 返回上一个结点
    35         return results

    当然我在Leetcode里发现了更简洁,漂亮的解法。如下:

    # the idea is if you switch head, the possible difference between length would be countered. 
    # On the second traversal, they either hit or miss. 
    # if they meet, pa or pb would be the node we are looking for, 
    # if they didn't meet, they will hit the end at the same iteration, pa == pb == None, return either one of them is the same,None

     即遍历到链表尾后就交换头结点继续遍历。在第二次反转时就会相遇或者错过。如果相遇那么该结点就是交叉点,错过就是不存在交叉点

     1 class Solution:
     2     # @param two ListNodes
     3     # @return the intersected ListNode
     4     def getIntersectionNode(self, headA, headB):
     5         if headA is None or headB is None:
     6             return None
     7 
     8         pa = headA # 2 pointers
     9         pb = headB
    10 
    11         while pa is not pb:
    12             # if either pointer hits the end, switch head and continue the second traversal, 
    13             # if not hit the end, just move on to next
    14             pa = headB if pa is None else pa.next   # 走到该链表末尾后下一步走另个链表开头
    15             pb = headA if pb is None else pb.next
    16 
    17         return pa # only 2 ways to get out of the loop, they meet or the both hit the end=None
  • 相关阅读:
    vue-router(路由)详细教程
    vue路由对象($route)参数简介
    this.$router.push相关的vue-router的导航方法
    es6 Promise.reject()方法
    百度阿里网易大疆等大小厂前端校招面筋 | 掘金技术征文
    Vue 脱坑记
    (尚016)Vue指令(11个自带指令+自定义指令)
    (尚015)Vue过滤器(对显示的数据进行格式化)
    (尚014)Vue过渡与动画
    (尚013)Vue的生命周期
  • 原文地址:https://www.cnblogs.com/king-lps/p/10659570.html
Copyright © 2011-2022 走看看