Leetcode +160 Intersection of Two Linked Lists

news/2024/7/7 16:12:55

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:

img

begin to intersect at node c1.

Example 1:

img

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:

img

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:

img

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.

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/intersection-of-two-linked-lists
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路解析
class Solution(object):
    def getIntersectionNode(self, headA, headB):
        ha, hb = headA, headB
        while ha != hb:
            ha = ha.next if ha else headB
            hb = hb.next if hb else headA
        return ha

headB
hb = hb.next if hb else headA
return ha



http://www.niftyadmin.cn/n/1051244.html

相关文章

Maven项目初开发(一)Maven项目开发的初配置(1)

首先一个五脏俱全的eclipse是必须的,J2EE版的eclipse一般都是整合了Maven插件。 Maven缺省的本地仓库地址为${user.home}/.m2/repository 。也就是说,一个用户会对应的拥有一个本地仓库。当然你可以通过修改${user.home}/.m2/settings.xml 配置这个地址…

妮可妮可妮 [Hash]

妮可妮可妮 题目描述 小P特别喜欢动画Love Live中的角色妮可,每当他听到妮可说“niconiconi”时,他总会感到特别兴奋,还会露出绅士般的微笑。 作为一名理论计算机科学家,小P开始研究“niconiconi”这种串的特点。他发现&#xff0…

Leetcode +169 Majority Element

Leetcode 169 Majority Element 题目描述 Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in t…

Maven项目初开发(一)Maven项目开发的初配置(2)

上一章讲了Maven的基本概念,下面讲操作Maven实现第一个Hello world。 Maven的设置 首先在window--preference--maven-user setttings中配置maven的settings.xml位置和本地仓库地址。新建Maven项目 配置完成之后,new maven project。不要选中Create a si…

第十三篇:软件包管理

软件包介绍 编译:源码转二进制(tar.gz源码包->rpm软件包) rpm软件包管理 1.安装软件包:rpm -i 软件路径 rpm -ivh 软件路径(vh:安装时查看安装信息和进度) rpm -i…

python基础10——函数初识

一、为什么要使用函数 #1、代码的组织结构不清晰,可读性差  #2、遇到重复的功能只能重复编写实现代码,代码冗余  #3、功能需要扩展时,需要找出所有实现该功能的地方修改之,无法统一管理且维护难度极大 二、函数的分类 内置函数…

JAVA随笔篇三(读写Properties配置文件以及路径问题详解)

首先记录下读写配置文件的方法,很简单的两个方法,主要就是路径问题,稍后会详细解释。 1、读Properties配置文件 public static void loadConfig(Properties Config, String filename) {InputStream in null;in FileUtil.class.getClassLoa…

Leetcode +206 Reverse Linked List

Leetcode 206 Reverse Linked List 题目描述 Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULLFollow up: A linked list can be reversed either iteratively or recursively. Could you …