链表查找中间节点_JavaScript。链表。 查找中间节点。

这篇博客探讨了如何在JavaScript中找到链表的中间节点。内容来源于对Medium上一篇关于链表操作的文章的翻译,讲解了在数据结构中处理链表的重要算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链表查找中间节点

Hello everyone! Today we have great chance to dive more into linked list and its functionality. I hope you already know basics about linked list and methods that it has. Today, we are going to discuss important method that you might be asked on the interview and I will be happy to share with you. Also, if you have just started learning about linked list, I would recommend to begin your path from here:

大家好! 今天,我们有很大的机会更深入地研究链表及其功能。 我希望您已经了解有关链表和链表方法的基础知识。 今天,我们将讨论面试中可能会问您的重要方法,我很乐意与您分享。 另外,如果您刚刚开始了解链表,建议您从这里开始:

查找中间节点 (Find Middle Node)

方向 (Directions)

Return the middle node of a linked list. If the list has an even number of elements, return the node at the end of the first half of the list.Do not use a counter variable, do not retrieve the size of the list, and only iterate through the list one time.

返回链表的中间节点。 如果列表中的元素数为偶数,则在列表前半部分的末尾返回节点。不要使用计数器变量,不要获取列表的大小,只遍历列表一次。

(Example)

const l = new LinkedList();
l.insertLast('a')
l.insertLast('b')
l.insertLast('c')
l.insertLast('d');
l.insertLast('e');midpoint(l); // returns {data: 'c'}

Let’s begin. I would like to start with 5 nodes(it’s our example for now).

让我们开始。 我想从5个节点开始(这是我们现在的示例)。

Image for post

We also create two variables which help us to iterate through all list. One variable is called slow and another one is fast and we point to the first node both of them. Why is like that ?

我们还创建了两个变量来帮助我们遍历所有列表。 一个变量称为慢变量,另一个变量称为快变量,我们都指向第一个节点。 为什么会这样?

Image for post

The reason is the ‘fast’ variable is going faster in two times than ‘slow’ one, because ‘slow’ will be on the half of the way of the ‘fast’ variable and it will be our middle of the list(‘slow’ will be in the middle of the list in the end of iterating).

原因是'fast'变量的运行速度比'slow'变量快两倍,因为'slow'处于'fast'变量的一半,它将位于列表的中间('slow' '将在迭代末尾位于列表的中间)。

Image for post

ALWAYS ‘fast’ checking condition: “If the next two elements are not null”. If it’s null — stop iterating. If not null continue iterating.

始终“快速”检查条件:“如果接下来的两个元素不为空”。 如果为null,请停止迭代。 如果不为null,则继续迭代。

Image for post

In this case it will return ‘slow’ with {data: ‘red’}.

在这种情况下,它将以{data:'red'}返回“ slow”。

We created the function ‘midpoint’ . ‘slow’ and ‘fast’ points to first element in the list. While ‘fast’ element has data which is not null till that time we will iterate. And in the end return slow which points our middle element.

我们创建了函数'midpoint'。 “慢”和“快”指向列表中的第一个元素。 虽然'fast'元素的数据直到那个时候都不为空,我们才会进行迭代。 最后返回慢点,这指向我们的中间元素。

Image for post
Image for post
Image for post

Full Code:

完整代码:

class Node {
constructor(data, next = null) {
this.data = data;
this.next = next;
}
}class LinkedList {
constructor() {
this.head = null;
}insertFirst(data) {
const node = new Node(data, this.head);
this.head = node;
}

size() {
let counter = 0;
let node = this.head;
while (node) {
counter++;
node = node.next;
}
return counter;
}

getFirst() {
return this.head.data;
}

getLast() {
if (!this.head) {
return null;
}
let node = this.head;
while (node) {
if (!node.next) {
return node;
}
node = node.next;
}
}

clear() {
this.head = null;
}

removeFirst() {
if (!this.head) {
return;
}
this.head = this.head.next;
}removeLast() {
if (!this.head) {
return;
}

if (!this.head.next) {
this.head = null;
}

let previous = this.head;
let node = this.head.next;

while (node.next) {
previous = node;
node = node.next;
}

previous.next = null;
} insertLast(data) {
const last = this.getLast();
if (last) {
last.next = new Node(data);
} else {
this.head = new Node(data);
}
}

getAt(index) {
let counter = 0;
let node = this.head;
while (node) {
if (counter === index) {
return node;
}
counter++;
node = node.next;
}
return null;
} removeAt(index) {
if (!this.head) {
return;
} if (index === 0) {
this.head = this.head.next;
return;
}

const previous = this.getAt(index - 1);
if (!previous || !previous.next) {
return;
}
previous.next = previous.next.next;
} insertAt(data, index) {
if (!this.head) {
this.head = new Node(data);
return;
}
if (index === 0) {
this.head = new Node(data, this.head);
return;
}
const previous = this.getAt(index - 1) || this.getLast();
const node = new Node(data, previous.next);
previous.next = node;
}
}function midpoint(list) {
let slow = list.head;
let fast = list.head;
while (fast.next && fast.next.next) {
slow = slow.next;
fast = fast.next.next;
}

return slow;
}const l = new LinkedList();
l.insertLast("a");
l.insertLast("b");
l.insertLast("c");
l.insertLast("d");
l.insertLast("e");console.log(midpoint(l)); // returns {data: 'c'}

翻译自: https://medium.com/dev-genius/javascript-linked-lists-find-middle-node-6b1fa73b20fb

链表查找中间节点

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值