HCL Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
The above is the correct solution, there is no need to move from your current pointer.
The only problem is the memory management. If you are in C++, then you just left that memory block allocated and have no way to reference it for the delete function to clean it up. If you are using Java, then the de-referencing will mark the 4th node for garbage collection.
This would be a very excellent call out to demonstrate your understanding of appropriate memory management in your given language.
ptr->next->next->next^=ptr->next->next;
free(ptr->next->next);
ptr->next->next^=ptr->next->next->next
// TODO: Handle next's == NULL
ptr->next = ptr->next->next->next;
delete ptr->next->next;
- Anonymous August 31, 2013