Skip to content
Related Articles

Related Articles

Javascript Program For Writing A Function To Delete A Linked List

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 09 Dec, 2021

A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers. This article focuses on writing a function to delete a linked list.



// Javascript program to delete
// a linked list
// Head of the list
var head;
// Linked List node
class Node
    { = val; = null;
// Function deletes the entire
// linked list
function deleteList()
    head = null;
// Inserts a new Node at front
// of the list.
function push(new_data)
    /* 1 & 2: Allocate the Node &
              Put in the data */
    var new_node = new Node(new_data);
    // 3. Make next of new Node as head = head;
    // 4. Move the head to point to new Node
    head = new_node;
// Use push() to construct list
// 1->12->1->4->1
document.write("Deleting the list<br/>");
document.write("Linked list deleted");
// This code contributed by Rajput-Ji


Deleting linked list
Linked list deleted

Time Complexity: O(n) 
Auxiliary Space: O(1)

Please refer complete article on Write a function to delete a Linked List for more details!

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!