文章目录
思路
解题方法
复杂度
- 时间复杂度:
- 空间复杂度:
Code
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
//Time Complexity: O(n)
//Space Complexity: O(1)
public ListNode deleteDuplicates(ListNode head) {
if (head == null) return null;
ListNode dummyNode = new ListNode(Integer.MAX_VALUE,head);
ListNode tail = dummyNode;
while (tail.next != null && tail.next.next != null) {
//出现重复
if (tail.next.val == tail.next.next.val) {
ListNode noReptition = tail.next;
while (noReptition != null && noReptition.val == tail.next.val) {
noReptition = noReptition.next;
}
tail.next = noReptition;
} else {
tail = tail.next;
}
}
return dummyNode.next;
}
}