

This sort of function that takes an element and searches the structure for it is best used for hashes with random access. It's not obvious what functionality this class offers over, say, a primitive array.Ĭonsider remove(item). This is debatable, because it restricts your internal logic from distinguishing between null and undefined or other falsey values, but if you trust yourself to be consistent about it, I prefer the cleaner look.Īs written, I find your provided function interface insufficient for typical linked list needs.
#MUTATE A LINKED LIST STACK OVERFLOW CODE#


throw "Wrong index" is an unclear error message.In this case, something like newNode might be clearer. Avoid single-letter variable names like p unless the meaning is obvious.All JS naming should be camelCase, except class names, which are UpperCamelCase (as you use). Function names switch between snake_case and camelCase.Generally, I don't see reason to deviate from this prescribed style. Your whitespace fixes can be done by putting the code into SE's Code Snippet and pressing "Tidy", then adding blank lines around blocks by hand. Writing a linked list class in JS only makes sense from an educational standpoint primitive arrays are efficient, universal and offer more functionality with less code.This class is missing basic add/remove functions which prevent it from being useful.Style could better adhere to established standards.For me, my logic makes sense, but I cannot figure out why it's not giving me the appropriate result.Invite reviews please on all fronts, improvements, modern usage of JS. I have tried many ways of writing my function and researched quite extensively about the subject and all answers I can find are dealing with nested lists (not linked lists implementation like mine).

Return Link(f(lnk.first), map_lnk(f, lnk.rest)) # Checks to see if its a pointer to another nested linked list Map all linked list elements according to the function provided. This is my current function for such operation: def map_lnk(f, lnk): Now comes the part I am having trouble with. The expected return value should be a linked list with the following values: ' 9 16 ]' So let's say that the function I would be using is lambda x: x*x. I would like to mutate all values of my linked list (respecting the nested order) according to a passed function. In a more visual manner, my declared linked list lnk would be: ' 3 4 ]' Say I have the following linked list implementation in Python with the following linked list as an example. I cannot find a way to mutate a linked list's elements according to a provided function.
