[Leetcode] [JavaScript] Delete Node in a Linked List

Source: Internet
Author: User

Delete Node in a Linked List

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.

Supposed the linked list 1 -> 2 -> 3 -> 4 is and you were given the third node with value 3 , the linked list should become 1 -> 2 -> 4 a Fter calling your function.

https://leetcode.com/problems/delete-node-in-a-linked-list/

Deletes a given node in a single-linked list, but does not give the table header of the linked list.

The solution is simple, the value of the latter node is overwritten with the value of the current node, and finally, the last node is deleted.

1 /**2 * @param {listnode} node3 * @return {void} does not return anything, modify node In-place instead.4  */5 varDeletenode =function(node) {6     varPrevious =node;7 Delnode (node.next);8     return;9     Ten     functionDelnode (n) { OnePrevious.val =N.val; A         if(N.next = = =NULL){ -Previous.next =NULL; -}Else{ thePrevious =Previous.next; -n =N.next; - Delnode (n); -         } +     } -};

[Leetcode] [JavaScript] Delete Node in a Linked List

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.