Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
Example 1:
Input: 1->2->3->3->4->4->5
Output: 1->2->5
Example 2:
Input: 1->1->1->2->3
Output: 2->3
Accepted on First Attempt 🍺
We try and use a pointer to a pointer. This allows handling special cases like updating head more generically although the approach may be off-putting to some.
Trying it on paper with a picture paid off. There wasn’t even a compilation error 🕺
1 | /** |
There is a flaw in this solution though. Can you see it?
Where’s my memory? 🕵
That skipDups()
function only skips but doesn’t release any memory!
1 | class Solution { |