Open GH1995 opened 5 years ago
fast = slow->next
fast
负责跑完所有的重复元素,当fast
停留在最后一个重复元素上时slow->next = fast->next
detect + +
| |
+-------+ +-------+ | +-------+ +-------+ | +-------+
| | | | | | 3 | | 3 | | | |
+-------+ +-------+ | +---^---+ +-------+ | +-------+
| + |
dummy slow | fast |
| |
| |
run over | |
| |
+-------+ +-------+ | +-------+ +-------+ | +-------+
| | | | | | 3 | | 3 | | | |
+-------+ +-------+ | +-------+ +---^---+ | +-------+
| + |
dummy slow + fast +
+---------------------------->
delete
slow->next = fast->next
这里代码也是要看的
设置了
slow
监察上一个不同的元素fast
跑所有相同的元素fast
处理有重复的情况fast
跑完了的情况
slow->next= fast->next
slow
自己跑一步
82. Remove Duplicates from Sorted List II
Difficulty: Medium
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
Example 1:
Example 2:
Solution
Language: C++