What is your hint or solution suggestion?
var Node = function(data, prev) {
this.data = data;
this.prev = prev;
this.next = null;
};
var DoublyLinkedList = function() {
this.head = null;
this.tail = null;
// change code below this line
this.add = function(element){
// create node
const newNode = new Node(element, this.tail);
// if list is empty
if (!this.head) {
this.head = newNode;
this.tail = newNode;
} else {
const previous = this.tail;
previous.next = newNode;
newNode.prev = previous;
this.tail = newNode;
}
}
// remove method
this.remove = function(element){
if (!this.head) return null;
let pointer = this.head,
previousNode = this.head.prev,
nextNode = this.head.next;
while(pointer !== null){
if (pointer.data === element){
if (pointer === this.head) {
this.head = pointer.next
break;
} else if (pointer === this.tail) {
this.tail = pointer.prev
this.tail.next = null;
console.log(this.tail)
break;
};
if (previousNode) previousNode.next = nextNode;
if (nextNode) nextNode.prev = previousNode;
}
pointer = pointer.next;
}
}
// change code above this line
};
Challenge: Data Structures: Create a Doubly Linked List
Link to the challenge: https://www.freecodecamp.org/learn/coding-interview-prep/data-structures/create-a-doubly-linked-list