简体   繁体   中英

How to iterate through a linked list in javascript

Someone shared this beautifully elegant way to create a linked list from an array.

 function removeKFromList(l, k) { let list = l.reduceRight((value, next)=>({next, value}), null); console.log(list); } let l = [3, 1, 2, 3, 4, 5]; let k = 3; removeKFromList(l, k); 

It's pretty easy to iterate arrays (for, filter, map, reduce, etc.) but a linked list has none of those features. I need to iterate through to remove k values from the list. I'm guessing I need to create a variable for the current node and use a while loop, but there's no documentation on this. I've seen some repl code doing it but it seems unnecessarily complicated.

How do you iterate through a linked list in javascript?

First of all, although the idea of using reduce is indeed beautiful, I must say that the result is not so good, because the resulting nodes have a field "next" where the value is and a field "value" where the next is, ie they are swapped. So let's fix that:

function removeKFromList(l, k) {
    let list = l.reduceRight((value, next)=>({value: next, next: value}), null);
    console.log(list);
}

Secondly, the name of that function is awful, it should be named "arrayToLinkedList" or something more suggestive. Also, logging the result does not make sense, we should return it instead. Moreover, the parameter k is simply unused. Fixing those things:

function arrayToLinkedList(array) {
    return array.reduceRight((prev, cur) => ({ value: cur, next: prev }), null);
}

Now, let's work on how to iterate over this. What do you think? I will give some hints because giving the answer directly probably won't help you learn much:

Observe that the linked list itself is:

  • Either the value null , or
  • A plain object with two fields, one field "value" which can be anything, and one field "next" which is a linked list.

Observe that the above (recursive) definition is well-defined and covers all cases.

Now, use that to your assistence. How do you get the first value? That would be simply myList.value , right? Now how do I get the second value? By applying .next , we get the next linked list, and so on. If next is null, you know you have to stop.

Let me know if you need further assistance.


EDIT: I noticed that you're looking for the right way to make an iterating method on lists with an idea of "adding it to the prototype" or something. So, about that:

To add an instance method by a prototype, you'd need your linked lists to be a class. That would be overcomplicating, unless you really have a good reason to define a class for this (which would be creating several methods and utility things around it).

It is much better, in my opinion, to just define a function that takes one linked list as the first parameter and a callback function as the second parameter, similarly to lodash's .each :

function forEachValueInLinkedList(linkedList, callback) {
     // here you loop through all values and call
     // callback(value)
     // for each value.
}

I'd say this would be the most "javascriptonic" way to do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM