Home > Back-end >  Delete nodes in singly linked list javascript
Delete nodes in singly linked list javascript

Time:10-26

I have a list of x numbers that I need to evaluate which are higher than 50 and delete those from the list and then print out the new list. I can't figure out how to add the condition >50 because it renders a blank array. Also, is there a more time efficient way to write this? Appreciate all the help. I'm very confused.

class Node {
  constructor(d) {
      this.data = d;
      this.next = null;
  }
}

class LinkedList {
  constructor(head = null) {
    this.head = head
  }
}

let firstNode = new Node(38);
let list = new LinkedList(firstNode);

let secondNode = new Node(74);
firstNode.next = secondNode;

let thirdNode = new Node(87);
secondNode.next = thirdNode;

let fourthNode = new Node(21);
thirdNode.next = fourthNode;

let fithNode = new Node(91);
fourthNode.next = fithNode;

let sixthNode = new Node(98);
fithNode.next = sixthNode;

let seventhNode = new Node(73);
sixthNode.next = seventhNode;

let eigthNode = new Node(41);
seventhNode.next = eigthNode;

let nithNode = new Node(29);
eigthNode.next = nithNode;

let tenthNode = new Node(3);
nithNode.next = tenthNode

let eleventhNode = new Node(72);
tenthNode.next = eleventhNode;

let twelthNode = new Node(94);
eleventhNode.next = twelthNode;

let thirteenthNode = new Node(12);
twelthNode.next = thirteenthNode;

let fourteenthNode = new Node(13);
thirteenthNode.next = fourteenthNode;

let fiftheenthNode = new Node(16);
fourteenthNode.next = fiftheenthNode;

let currentNode = list.head;

while (currentNode != null && currentNode > 50) {
  console.log(currentNode.data);
  currentNode = currentNode.next;
}

CodePudding user response:

You need to make exceptions for the head, and the loop you're using should actually be used for deleting inside another loop that goes through all the nodes (so the outer loop cannot filter by currentNode.data > 50).

I've edited your code so that it should behave more as you were asking for:

    class Node {
        constructor(d) {
            this.data = d;
            this.next = null;
        }
    }

    class LinkedList {
        constructor(head = null) {
            this.head = head
        }

        //for printing
        printable(sll) {
            if (this.head == null) {
                return 'Empty list';
            }
            let sll_str = `${this.head.data}`;
            let cNode = this.head.next;
            while (cNode != null) {
                sll_str  = `, ${cNode.data}`;
                cNode = cNode.next;
            }
            return sll_str;
        }
    }


    let firstNode = new Node(38);
    let list = new LinkedList(firstNode);

    let secondNode = new Node(74);
    firstNode.next = secondNode;

    let thirdNode = new Node(87);
    secondNode.next = thirdNode;

    let fourthNode = new Node(21);
    thirdNode.next = fourthNode;

    let fithNode = new Node(91);
    fourthNode.next = fithNode;

    let sixthNode = new Node(98);
    fithNode.next = sixthNode;

    let seventhNode = new Node(73);
    sixthNode.next = seventhNode;

    let eigthNode = new Node(41);
    seventhNode.next = eigthNode;

    let nithNode = new Node(29);
    eigthNode.next = nithNode;

    let tenthNode = new Node(3);
    nithNode.next = tenthNode

    let eleventhNode = new Node(72);
    tenthNode.next = eleventhNode;

    let twelthNode = new Node(94);
    eleventhNode.next = twelthNode;

    let thirteenthNode = new Node(12);
    twelthNode.next = thirteenthNode;

    let fourteenthNode = new Node(13);
    thirteenthNode.next = fourteenthNode;

    let fiftheenthNode = new Node(16);
    fourteenthNode.next = fiftheenthNode;

    let currentNode = list.head;

    console.log("old list: ", list.printable());
    while (currentNode != null) {
        if (list.head == currentNode && currentNode.data > 50) {
            list.head = currentNode.next;
            console.log("deleting ", currentNode.data);
            let toDel = currentNode;
            delete (toDel);
        } else {
            console.log(currentNode.data);
            let newNext = currentNode.next;
            while (newNext != null && newNext.data > 50) {
                let toDel = newNext;
                newNext = newNext.next;
                console.log("deleting ", toDel.data);
                delete(toDel);
            }
            currentNode.next = newNext;
        }
        currentNode = currentNode.next;
    }
    console.log("\nnew list: ", list.printable());

(I also added a function to the LinkedList class to make it easier to print.)

CodePudding user response:

This can be done with looping on the created Linkedlist and if the value is less than equal 50 than put it into new linkedlist and map it. The last console is to print out the actual linkedlist, because intially we created dummyHead with Infinity value so our result starts from the next of resultHead.

class Node {
  constructor(d) {
      this.data = d;
      this.next = null;
  }
}

class LinkedList {
  constructor(head = null) {
    this.head = head
  }
}

let firstNode = new Node(38);
let list = new LinkedList(firstNode);

let secondNode = new Node(74);
firstNode.next = secondNode;

let thirdNode = new Node(87);
secondNode.next = thirdNode;

let fourthNode = new Node(21);
thirdNode.next = fourthNode;

let fithNode = new Node(91);
fourthNode.next = fithNode;

let sixthNode = new Node(98);
fithNode.next = sixthNode;

let seventhNode = new Node(73);
sixthNode.next = seventhNode;

let eigthNode = new Node(41);
seventhNode.next = eigthNode;

let nithNode = new Node(29);
eigthNode.next = nithNode;

let tenthNode = new Node(3);
nithNode.next = tenthNode

let eleventhNode = new Node(72);
tenthNode.next = eleventhNode;

let twelthNode = new Node(94);
eleventhNode.next = twelthNode;

let thirteenthNode = new Node(12);
twelthNode.next = thirteenthNode;

let fourteenthNode = new Node(13);
thirteenthNode.next = fourteenthNode;

let fiftheenthNode = new Node(16);
fourteenthNode.next = fiftheenthNode;

let currentNode = list.head;
let resultNode = new Node(-Infinity);
let resultHead = resultNode;
while (currentNode != null) {
  //console.log(currentNode.data);
  if(currentNode.data <= 50){
    resultNode.next = new Node(currentNode.data);
    resultNode = resultNode.next;
  }
  currentNode = currentNode.next;     
}
console.log(resultHead.next);

CodePudding user response:

I am happy that you choose Javascript for LinkedList like me. I will provide you with the Solution, please refer to this :

function deleteNode(position)
{
     
    // If linked list is empty
    if (head == null)
        return;
     
    // Store head node
    var temp = head;
     
    // If head needs to be removed
    if (position == 0)
    {
         
        // Change head
        head = temp.next;
        return;
    }
     
    // Find previous node of the node to be deleted
    for(i = 0; temp != null && i < position - 1; i  )
        temp = temp.next;
     
    // If position is more than number of nodes
    if (temp == null || temp.next == null)
    return;
     
    // Node temp->next is the node to be deleted
    // Store pointer to the next of node to be deleted
    var next = temp.next.next;
     
    // Unlink the deleted node from the list
    temp.next = next;
}

Here you can pass position = position of the linked list element you want to delete.

Just implement the below code firstly with paper and pen after that you will understand this.

For any confusion, please let me know by commenting here.

Happy Coding !!

  • Related