leetcode题目地址
一般做法
时间复杂度O(n)
空间复杂度O(1)
public ListNode DeleteDuplicates(ListNode head) {
if (head == null) return head;
ListNode current = head;
while (current.next != null)
{
if (current.next.val == current.val)
{
current.next = current.next.next;
}
else
{
current = current.next;
}
}
return head;
}
递归解法
时间,空间都是0(n)
if (head == null) return head;
head.next = DeleteDuplicates(head.next);
return head.val == head.next.val ? head.next : head;