Tell us what’s happening:
I am confused on what “element” exactly is?
Isn’t it a key:value pair, which is included in every single “next:” element.
I found another user using the method which succeeds in the end (1st in the function). My solution removes the “else” block and doesn’t succeed, even when returning false by default.
Can someone please explain what the “element” actually is and if this is a bug or not? because I am genuinely confused
Your code so far
function initList() {
return {
head: null,
length: 0
};
}
function isEmpty(list) {
return list.length === 0;
}
function add(list, element) {
const node = { element, next: null };
if (isEmpty(list)) {
list.head = node;
} else {
let current = list.head;
while (current.next !== null) {
current = current.next;
}
current.next = node;
}
list.length++;
}
function remove(list, element) {
let previous = null;
let current = list.head;
while (current !== null && current.element !== element) {
previous = current;
current = current.next;
}
if (current === null) return;
if (previous !== null) {
previous.next = current.next;
} else {
list.head = current.next;
}
list.length--;
}
function contains(list, element) {
let currEl = list.head;
/* This makes Test #6 SUCCEED, even when it doesn't cycle through every "next:" Element
while (currEl){
currEl = currEl.next;
if (currEl.element === element){
return true;
} else{
return false;
}
}
*/
/* This makes Test #6 FAIL, even though it cycles through every "next:" Element and triggers: "return false" in the end
while (currEl){
currEl = currEl.next;
if (currEl.element === element){
return true;
}
}
*/
return false;
}
function getAt(list, index) {
if (index < 0 || list.length === 0){
return undefined;
}
let currList = list.head;
while (index > 0){
currList = currList.next;
if (!currList){
return undefined;
}
index--;
}
return currList.element;
}
function insertAt(list, index, element) {
if (index < 0 || list.length < index) return;
const node = {element, next: null};
if (index === 0){
node.next = list.head;
list.head = node;
list.length++;
return;
}
let listEl = list.head;
let previous;
while (index > 0){
previous = listEl;
listEl = listEl.next;
index--;
}
previous.next = node;
node.next = listEl;
node.element = element;
list.length++;
}
function removeAt(list, index) {
if (index < 0 || list.length < index) return;
}
function clear(list) {
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:152.0) Gecko/20100101 Firefox/152.0
Challenge Information:
Implement Linked List Operations - Implement Linked List Operations