0

I'm working on this leetcode problem: https://leetcode.com/problems/delete-node-in-a-linked-list/

Write a function to delete a node in a singly-linked list. You will not be given access to the head of the list, instead you will be given access to the node to be deleted directly.

It is guaranteed that the node to be deleted is not a tail node in the list.

and I've found this solution:

var deleteNode = function (node) {
    node.val = node.next.val;
    node.next = node.next.next;
};

This is my Linked List implementaion:

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

class LinkedList {
    constructor(value) {
        const newNode = new Node(value);
        this.head = newNode;
        this.tail = this.head;
        this.length = 1;
    }
}

I dont't know why it's not working on my LL implemetation.

4
  • @RomainHippeau You can't do that in a singly-linked list without being given the head pointer. Commented Oct 25, 2021 at 15:09
  • @RomainHippeau He's copying the value from the next one into the current one before deleting the next. Commented Oct 25, 2021 at 15:10
  • If you delete the 2nd-to-last node, it becomes the tail, but LinkedList.tail still points to the previous tail. Commented Oct 25, 2021 at 15:12
  • And you also don't reduce the length of the LinkedList that contains it. Commented Oct 25, 2021 at 15:12

1 Answer 1

2

In the referenced question there is just this node class being used. This is what Leet Code shows in a comment block:

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */

The first issue is that the val property is called value in your class, so you should change that name where it occurs in the deleteNode code.

Secondly, Leet Code does not introduce nor need a linked list class, so that means that all the data you have in your LinkedList instance, will have to be kept updated on top of what you would do with just a node class, like the one above.

Actually, by adding a LinkedList class with its own properties, you'll have an additional problem: when deleteNode(node) is called, you'll have to be sure that node is actually a node in your linked list instance, and not in some other linked list. What if you have two linked lists? How will you know to which list the node belongs that is passed as argument to deleteNode?

Now if we can assume that the node argument is a node in your current linked list, we can define deleteNode as a method.

A side note: your LinkedList constructor immediately creates a node, but this is a bad idea: linked lists can be empty, so start with an empty list (by default), and use the usual addition method(s) to also add the first node.

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

class LinkedList {
    constructor() {
        // A linked list could be empty, so don't create a node
        this.head = null;
        this.tail = null;
        this.length = 0;
    }
    append(val) {  // 
        if (!this.tail) this.head = this.tail = new Node(val);
        else this.tail = this.tail.next = new Node(val);
        this.length++;
    }
    deleteNode(node) {
        node.value = node.next.value; // Use value, not val
        node.next = node.next.next;
        // Also update the linked list properties:
        if (!node.next) this.tail = node;
        this.length--;
    }
    *[Symbol.iterator]() { // A generator to easily output the list's values
        for (let node = this.head; node; node = node.next) {
            yield node.value;
        }
    }
}

// demo
const list = new LinkedList(); 
list.append(1);
list.append(2);
list.append(3);
console.log("initial list:  ", ...list);
list.deleteNode(list.head.next); // Delete the 2
console.log("after deletion:", ...list);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.