What is need to create extra variable?

Why are we creating temp? can’t we just head pointer to insert and traverse linked list?

Hello @itsmeshubhamkumar,

The only reason to traverse using a temporary variable instead of head itself is to preserve the address the starting node.

If you’ll traverse using head, then you’ll change the content of head.
The purpose of head pointer(conventionally) is to keep track of the starting of node of the linked list.

Hope, this would help.
Give a like, if you are satisfied.

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.