Linked List Remove Nodes
The question
Given a linked list of integers and an integer value, delete every node of the linked list containing that value.
The solution
The tricky part about this problem is taking care of the edge cases. You have to go through all the elements in the list, so the efficiency is O(n).
- Initialize one variable(lastGood) to null
- Initialize one variable(first) to the head
- Check the first element on the linked list(current)
- If it is not the number you are looking for assign lastGood=current and move current to the next node
- If it is the number then check if lastGood has a value assigned
- if lastGood doesn’t have a value it means we want to remove the first item so assign first to current.next
- if lastGood has a value then assign lastGood.next to current.next
- Repeat for all elements