[LeetCode] 876. Middle of the Linked List

2023-05-16

Given a non-empty, singly linked list with head node head, return a middle node of linked list.
If there are two middle nodes, return the second middle node.
Example 1:
Input: [1,2,3,4,5]
Output: Node 3 from this list (Serialization: [3,4,5])
The returned node has value 3. (The judge’s serialization of this node is [3,4,5]).
Note that we returned a ListNode object ans, such that:
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, and ans.next.next.next = NULL.
Example 2:
Input: [1,2,3,4,5,6]
Output: Node 4 from this list (Serialization: [4,5,6])
Since the list has two middle nodes with values 3 and 4, we return the second one.
Note:
The number of nodes in the given list will be between 1 and 100.
If there are two middle nodes, return the second middle node.

方法1: 先取长度再求中点

let middleNode = function(head) {
    let targetLen = Math.ceil(getLength(head)/2);
    return returnAtPosition(head, targetLen);
};

let getLength = function(node) {
    let length = 0;
    while(node.next) {
        length += 1;
        node = node.next;
    }
    return length;
};

let returnAtPosition = function(node, targetLen) {
    for(let i = 0; i < targetLen; i++) {
        node = node.next;
    }
    return node;
};

方法2:快慢指针

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var middleNode = function(head) {
  if (head === null) {
      return false; 
  }
    
  var slow = head;
  var fast = head;
  while (true) {
      if (fast.next === null) {
          return slow;
      }
      
      if (fast.next.next === null) {
          return slow.next;
      }
      
      fast = fast.next.next
      slow = slow.next
  }
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

[LeetCode] 876. Middle of the Linked List 的相关文章

随机推荐

  • 单目测距 视觉测距

    文章目录 单目测距在kitti数据集中的测试结果C 43 43 工程原理代码注释 其他视觉测距算法 基于相似三角形的单目测距算法原理代码 参考资料 单目测距 在kitti数据集中的测试结果 C 43 43 工程 C 43 43 工程代码下载
  • python json 解析

    coding utf 8 import sys os re class JsonBaseType single type 61 0 object type 61 1 array type 61 2 class ParseException
  • proto_cmake_test

    proto cmake test Proto与CMAKE结合编译源代码 工程编译 span class token function cd span build cmake span class token punctuation span
  • Opencv获取指定时间内的视频片段以及帧

    文章目录 源码编译运行 源码 span class token comment gt File Name ddd cpp gt Author gt Mail 1 64 163 com gt Created Time 2022年06月17日
  • Opencv将目录下的图片存储为视频

    文章目录 源码编译运行 源码 span class token comment gt File Name main cpp gt Author gt Mail 1 64 163 com gt Created Time 2022年06月17日
  • 机器学习之svm---车牌识别

    目标 团队 承接嵌入式linux软硬件开发 机器视觉 图像处理 网络流等项目 微信号 xff1a hgz1173136060本文档尝试解答如下问题 如何使用OpenCV函数 CvSVM train 训练一个SVM分类器 xff0c 以及用
  • I2C调试工具

    1 I2C调试工具 i2c tools工具是开源I2C调试工具 xff0c 具有获取I2C总线挂载的设备列表及设备地址 xff0c 可对指定设备指定寄存器进行读写的功能 ubuntu安装 xff1a apt get install libi
  • Ubuntu18版本安装ROS

    最近不小心把虚拟机里的ROS弄坏了 xff0c 导致Linux都无法使用 也忘了快照导致所以软件重新安装 xff0c 在这里给大家分享一下ubuntu18版本如何安装ros以及我安装中出现的问题如何进行解决 注 xff1a 不同的ubunt
  • ZED相机快速使用指南

    1 安装SDK ZED SDK 3 8 Download Stereolabs 2 安装ros GitHub stereolabs zed ros wrapper ROS wrapper for the ZED SDK 其他教程 xff1a
  • vscode:前进后退快捷键

    1 xff09 后退 xff1a alt 43 2 xff09 前进 xff1a alt 43
  • git: tag 和 branch 的区别

    前言 tag 是什么 tag 翻译过来是标签的意思 xff0c 顾名思义 xff0c 标签是为了标记某种事物 tag 是 Git 版本库的一个快照 xff0c 指向某个 commit 的指针 tag 的好处 tag 的存在 xff0c 是因
  • QApplication a(argc,argv);崩溃

    Microsoft Visual C 43 43 Debug Library Debug Error Program de mytoolkit mytoolkit mytoolkit Win32 Debug mytoolkit exe Mo
  • jetson Xavier nx安装torch和torchvision,并解决解决版本不匹配(报错RuntimeError: Couldn‘t load custom C++ ops)的问题

    目录 1 安装torch 2 安装torchvision 3 验证是否安装成功 4 错误记录 5 torch和torchvision网盘链接 首先 xff0c torch和torchvision都不能直接pip安装 xff0c 以下的演示是
  • C++ 数据结构:DS顺序表--合并操作

    题目描述 建立顺序表的类 xff0c 属性包括 xff1a 数组 实际长度 最大长度 xff08 设定为1000 xff09 已知两个递增序列 xff0c 把两个序列的数据合并到顺序表中 xff0c 并使得顺序表的数据递增有序 输入 第1行
  • ubuntu安装多个版本python

    背景 xff1a 本地环境 Ubuntu 22 04 64 bit xff0c 默认安装python3 10 6 xff0c 未安装pip venv 需求 xff1a 安装python3 8 xff0c 并安装两版本对应pip xff0c
  • socket编程中recv()和read()的使用与区别

    recv和read相似 xff0c 都可用来接收sockfd发送的数据 xff0c 但recv比read多了一个参数 xff0c 也就是第四个参数 xff0c 它可以指定标志来控制如何接收数据 1 recv 原型 xff1a ssize t
  • stm32printf函数的串口输出代码

    stm32f103串口一与串口二printf函数输出 因项目需要特意配置了该段代码 xff0c 不喜勿喷 xff0c 纯属个人笔记 对于串口的代码网上也是很多 xff0c 无非是配置问题 xff0c 该代码是基于stm32f103c8t6来
  • C/C++ —— 小端转大端函数的使用

    函数说明 uint32 t htonl uint32 t hostlong uint16 t htons uint16 t hostshort uint32 t ntohl uint32 t netlong uint16 t ntohs u
  • AntDesign Upload组件上传图片

    技术选型 前端技术选型 xff1a React Hook 43 typescript antd版本 xff1a 3 18 使用Upload上传图片 上传效果截图 预览效果截图 项目中完整写法 xff1a span class token k
  • [LeetCode] 876. Middle of the Linked List

    Given a non empty singly linked list with head node head return a middle node of linked list If there are two middle nod