Intuition
There are several stuff to notice:
- Remove the duplicates, keep no one of them
- Find the duplicates by three pointers: pre,cur and next, indicating previous node current node and next node. When the value of next node and cur node are equal, move next node to the one that not equal to cur node, and connect it to pre node, so the duplicates are removed.
Code
1 | /** |
Complexity
Time Complexity: $O(N)$ We use three pointers to iterate over the list.
Space Complexity: $O(1)$