Write a program to delete a linked list. Given a linked list, we have to write a code to delete a linked list.
To delete a complete linked list, traverse all the nodes of a linked list and delete one by one.
Print Middle Element of a Linked List
How to Delete a Linked List
To delete a node one by one maintain two pointers. The first pointer points to head and the second pointer keeps the reference to next node. So when a node is free, you can assign the reference of next node using the second pointer.
Write a program to reverse a linked list using recursion. Given a singly linked list, we have to write a code to reverse a linked list using recursion. This question is mostly asked in interviews.
In my previous post, I have explained how to reverse a linked list using an iterative approach. In this post, We will learn how to reverse a linked list using recursion. The time complexity for both iterative and recursive approach is O(n).
To insert node at the beginning of a linked list
1. Create a new node dynamically.
2. Insert a data and point the new node to the next node.
3. Put a reference of head node to the new node. So that head node points to new node.