xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

LeetCode 链表的中间结点算法题解 All In One

LeetCode 链表的中间结点算法题解 All In One

js / ts 实现重排链表

链表的中间结点原理 图解

// 快慢指针

function middleNode(head: ListNode | null): ListNode | null {
  // 快慢指针
  let slow = head;
  let fast = head;
  while(fast && fast.nex) {
    fast = fast.next.next;
    // slow 每次前进一步
    slow = (slow as ListNode).next;
  }
  return slow;
};

876. 链表的中间结点

"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2022-08-26
 * @modified
 *
 * @description 876. 链表的中间结点
 * @description 876. Middle of the Linked List
 * @difficulty Easy
 * @ime_complexity O(n)
 * @space_complexity O(n)
 * @augments
 * @example
 * @link https://leetcode.com/problems/middle-of-the-linked-list/
 * @link https://leetcode.cn/problems/middle-of-the-linked-list/
 * @solutions
 *
 * @best_solutions
 *
 */

export {};

const log = console.log;


// singly-linked list
class ListNode {
  val: number
  next: ListNode | null
  constructor(val?: number, next?: ListNode | null) {
    this.val = (val===undefined ? 0 : val)
    this.next = (next===undefined ? null : next)
  }
  // add
  // remove
}


function middleNode(head: ListNode | null): ListNode | null {
  // 快慢指针
  let slow = head;
  let fast = head?.next;
  while(fast && fast.next && fast.next.next) {
    // fast  每次前进两步
    fast = fast.next.next;
    // slow 每次前进一步
    slow = (slow as ListNode).next;
    // slow = slow.next;
    // Object is possibly 'null'.ts(2531)
  }
  return slow?.next || slow;
  // return slow?.next ? slow.next : slow;
};

/*

测试用例

[1,2,3,4,5]
[1,2,3,4,5,6]
[1]

 */

// 测试用例 test cases
const testCases = [
  {
    input: [1,2,3,4,5],
    result:[3,4,5],
    desc: 'value equal to [3,4,5]',
  },
  {
    input: [1,2,3,4,5,6],
    result: [4,5,6],
    desc: 'value equal to [4,5,6]',
  },
  {
    input: [1],
    result: [1],
    desc: 'value equal to [1]',
  },
];


function LinkedListGenerator(arr: number[]) {
  let head;
  const len = arr.length;
  for (let i = 0; i < len; i++) {
    if(i === 0) {
      head = new ListNode(arr[len - 1 - i], null);
    } else {
    let temp = new ListNode(arr[len - 1 - i], null);
      temp.next = head ;
      head = temp;
    }
  }
  // console.log(head);
  return head;
}

for (const [i, testCase] of testCases.entries()) {
  const head = LinkedListGenerator(testCase.input);
  const result = middleNode(head);
  const resultHead = LinkedListGenerator(testCase.result);
  log(`test case i result: \n`, JSON.stringify(result) === JSON.stringify(resultHead) ? `passed ✅` : `failed ❌`, result);
  // log(`test case i result: \n`, result.join('') === testCase.result.join('') ? `passed ✅` : `failed ❌`, result);
  // log(`test case i =`, testCase);
}


/*

$ npx ts-node ./876\ middle-of-the-linked-list.ts

*/

https://leetcode.com/problems/middle-of-the-linked-list/
https://leetcode.cn/problems/middle-of-the-linked-list/

LeetCode 题解 / LeetCode Solutions

https://www.youtube.com/results?search_query=+Leetcode+876

https://www.youtube.com/playlist?list=PLamwFu9yMruCBtS2tHUD77oI_Wsce-syE

YouTube & LeetCode 力扣官方算法题解视频列表

https://github.com/xgqfrms/leetcode/issues/14

https://www.youtube.com/channel/UCftIXZeipv4MTVwmfFphtYw/videos

https://neetcode.io/

类似问题

LeetCode 206. Reverse Linked List / 反转链表

https://leetcode.com/problems/reverse-linked-list/

LeetCode 92. Reverse Linked List II / 反转链表 2

https://leetcode.com/problems/reverse-linked-list-ii/

refs



©xgqfrms 2012-2025

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(31)  评论(3编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
历史上的今天:
2021-08-26 js white space length All In One
2021-08-26 Xbox 微软飞行模拟器 中文版 All In One
2020-08-26 如何正确的使用 Dart SDK API
2020-08-26 Dart & final vs const
2020-08-26 CSS3 Grid Layout & <track-size> & <line-name>
2020-08-26 UTM & User Tracking Message All In One
2019-08-26 css & circle & shapes
点击右上角即可分享
微信分享提示