Get the App
SLTechnology News&Howtos  ›  Development  › 

How to find linked list elements in js

Shulou Source: shulou.com Published: 2022-06-03 08:28:42 10月02日 Update

This article will explain in detail how to find linked list elements in js. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

1. Implement the indexOf method to return the given element to the index position in the linked list.

IndexOf (element) {/ / traverses from the head of the linked list until it finds the same element as the given element, and then returns the corresponding index number. If no corresponding element is found,-1 is returned. Let current = this.head; for (let I = 0; I

< this.length; i++) { if (current.element === element) return i; current = current.next; } return -1;} 2、实现find方法,用于查找链表中指定位置的节点。从链表的head开始,遍历整个链表,直到找到相应索引位置的节点,然后返回节点。 find(position) { //首先判断参数position的边界值,如果值超出了索引的范围(小于0或者大于length - 1),则返回null if (position < 0 || position >

= this.length) return null; / / starts with the head of the linked list, iterates through the linked list until the node at the corresponding index location is found, and then returns this node. Let current = this.head; for (let I = 0; I < position; iTunes +) {current = current.next;} return current;} on "how to find linked list elements in js" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Tags: Element index node location article method more good practical same middle finger content parameter header article knowledge index number range boundary reference Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Apple Microsoft NVidia OPPO Reno