2024-09-09 web, development, javascript

Adding to Removing from Traversing and Reversing the Linked List

By O. Wolfson

Read my previous tutorial on linked lists here.

Now that we have a basic implementation of a linked list, let's expand its functionality with additional methods for adding to, removing from, traversing, and reversing the list.

Add a new method prepend to add a node to the beginning of the list:

javascript
function createLinkedList() {
// ... previous code ...

prepend: function (data) {
const newNode = createNode(data);
newNode.next = this.head;
this.head = newNode;
},

// ... remaining code ...
}

Add a method remove to delete a node with the specified data from the list:

javascript
function createLinkedList() {
// ... previous code ...

remove: function (data) {
if (!this.head) {
return;
}

    if (this.head.data === data) {
      this.head = this.head.next;
      return;
    }

    let currentNode = this.head;
    while (currentNode.next && currentNode.next.data !== data) {
      currentNode = currentNode.next;
    }

    if (currentNode.next) {
      currentNode.next = currentNode.next.next;
    }

},

// ... remaining code ...
}

Add a method find to search for a node with the specified data and return it:

javascript
function createLinkedList() {
// ... previous code ...

find: function (data) {
let currentNode = this.head;
while (currentNode && currentNode.data !== data) {
currentNode = currentNode.next;
}

    return currentNode;

},

// ... remaining code ...
}

Add a method reverse to reverse the order of the nodes in the list:

javascript
function createLinkedList() {
  // ... previous code ...

  // Add a reverse method to the linked list
  reverse: function () {
    // Initialize the previousNode variable as null
    let previousNode = null;
    // Set the currentNode variable to the head of the linked list
    let currentNode = this.head;

    // Iterate through the linked list until currentNode becomes null
    while (currentNode) {
      // Store the next node in the list in the variable nextNode
      const nextNode = currentNode.next;
      // Set the currentNode's next pointer to point to the previous node
      currentNode.next = previousNode;
      // Move the previousNode pointer to the current node
      previousNode = currentNode;
      // Move the currentNode pointer to the next node
      currentNode = nextNode;
    }

    // Update the head of the linked list to be the previousNode (the new head)
    this.head = previousNode;

  },

  // ... remaining code ...
}

Now, let's use the new methods with our linked list:

javascript
const myList = createLinkedList();
myList.append("Alice");
myList.append("Bob");
myList.prepend("Charlie");
myList.printList(); // Outputs: Charlie, Alice, Bob

myList.remove("Alice");
myList.printList(); // Outputs: Charlie, Bob

const foundNode = myList.find("Bob");
console.log(foundNode); // Outputs: { data: 'Bob', next: null }

myList.reverse();
myList.printList(); // Outputs: Bob, Charlie

In this follow-up tutorial, we added new methods to our linked list implementation. The prepend method adds a node to the beginning of the list, the remove method deletes a node with the specified data, the find method searches for a node with the specified data and returns it, and the reverse method reverses the order of the nodes in the list.

Check out the previous tutorial on linked lists here.