<script>
var
head;
class Node
{
constructor(val)
{
this
.data = val;
this
.next =
null
;
}
}
function
deleteNode( node, n)
{
if
(node == n)
{
if
(node.next ==
null
)
{
document.write(
"There is only one node. The list "
+
"can't be made empty "
);
return
;
}
node.data = node.next.data;
n = node.next;
node.next = node.next.next;
return
;
}
prev = node;
while
(prev.next !=
null
&& prev.next != n)
{
prev = prev.next;
}
if
(prev.next ==
null
)
{
document.write(
"Given node is not present in Linked List"
);
return
;
}
prev.next = prev.next.next;
return
;
}
function
printList( head)
{
while
(head !=
null
)
{
document.write(head.data +
" "
);
head = head.next;
}
document.write(
""
);
}
head =
new
Node(12);
head.next =
new
Node(15);
head.next.next =
new
Node(10);
head.next.next.next =
new
Node(11);
head.next.next.next.next =
new
Node(5);
head.next.next.next.next.next =
new
Node(6);
head.next.next.next.next.next.next =
new
Node(2);
head.next.next.next.next.next.next.next =
new
Node(3);
document.write(
"Given Linked List :"
);
printList(head);
document.write(
""
);
document.write(
"<br/>Deleting node :"
+ head.next.next.data);
deleteNode(head, head.next.next);
document.write(
"<br/>Modified Linked list :"
);
printList(head);
document.write(
"<br/>"
);
document.write(
"Deleting first Node<br/>"
);
deleteNode(head, head);
document.write(
"Modified Linked List"
);
printList(head);
</script>