Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmJavascript Program For Writing A Function To Delete A Linked List

Javascript Program For Writing A Function To Delete A Linked List

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.

Implementation: 

Javascript




<script>
// Javascript program to delete
// a linked list
 
// Head of the list
var head;
 
// Linked List node
class Node
{
    constructor(val)
    {
        this.data = val;
        this.next = 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
    new_node.next = head;
 
    // 4. Move the head to point to new Node
    head = new_node;
}
 
// Use push() to construct list
// 1->12->1->4->1
push(1);
push(4);
push(1);
push(12);
push(1);
 
document.write("Deleting the list<br/>");
deleteList();
document.write("Linked list deleted");
// This code contributed by Rajput-Ji
</script>


Output:

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!
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Commit to GfG’s Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated :
09 Dec, 2021
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Share your thoughts in the comments

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments