During my recent course of practice with LinkedList , i experienced many problems are related to keeping track of right Head pointer of the list . Whether its a Swapping Nodes without swapping data or simple insertion and deletion of nodes.
Pointer : When dealing with Swapping Nodes , Insertion and Deletion at head , if the pointer to previous Node is NULL , then it means First Node is under consideration
This is how I approach :
Node* prevNode = NULL ;
Node* ptr = head;
for(int i = 0 ; i < position ; ++ i
{
prevNode = ptr;
ptr = ptr->next;
}
if( prevNode == NULL )
{
// manipulate head pointer here
}
Checkout Swapping Nodes without swapping data for a real life problem
Pointer : When dealing with Swapping Nodes , Insertion and Deletion at head , if the pointer to previous Node is NULL , then it means First Node is under consideration
This is how I approach :
Node* prevNode = NULL ;
Node* ptr = head;
for(int i = 0 ; i < position ; ++ i
{
prevNode = ptr;
ptr = ptr->next;
}
if( prevNode == NULL )
{
// manipulate head pointer here
}
Checkout Swapping Nodes without swapping data for a real life problem
No comments:
Post a Comment