Javascript Program For Writing A Function To Delete A Linked List
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.
Implementation:
Javascript
<script>
var head;
class Node
{
constructor(val)
{
this .data = val;
this .next = null ;
}
}
function deleteList()
{
head = null ;
}
function push(new_data)
{
var new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
push(1);
push(4);
push(1);
push(12);
push(1);
document.write( "Deleting the list<br/>" );
deleteList();
document.write( "Linked list deleted" );
</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!
Share your thoughts in the comments
Please Login to comment...