0
点赞
收藏
分享

微信扫一扫

【Python selenium过极验滑块】用自动化selenium 操作GEETEST极验滑块,简单粗暴

小北的爹 2024-07-25 阅读 16

【链表】 No. 0160 相交链表 【简单】👉力扣对应题目指路

题目描述:两个单链表的头节点 headA 和 headB ,请找出并返回两个单链表相交的起始节点。如果不存在相交节点,返回 null 。

  • 图示两个链表在节点 c1 开始相交:

# Definition for singly-linked list.
# 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
        """
         # ------------------------------------------------------------ step 1
        def get_len(head): 
            current = head
            result = 0
            while current:
                result += 1
                current = current.next
            return result

        current_A = headA
        current_B = headB

        len_A = get_len(current_A)
        len_B = get_len(current_B)

        L = abs(len_A-len_B)  # -------------------------------------- step 2

		# ------------------------------------------------------------ step 3
        if len_A > len_B:
            while L:
                current_A = current_A.next
                L -= 1
        if len_A < len_B:
            while L:
                current_B = current_B.next
                L -= 1
                
        while current_A:  # ------------------------------------------ step 4
            if current_A == current_B:
                return current_A
            current_A = current_A.next
            current_B = current_B.next
   
        return None  # ----------------------------------------------- step 5
举报

相关推荐

0 条评论