/**
* Runtime: 1 ms, faster than 28.08%
* Memory Usage: 41.2 MB, less than 5.44%
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode pre = head, curr = head.next;
while (curr != null) {
if (curr.val != pre.val) {
pre.next = curr;
pre = curr;
}
curr = curr.next;
}
pre.next = null;
return head;
}
}
/**
* Runtime: 2 ms, faster than 28.08%
* Memory Usage: 41 MB, less than 18.23%
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head == null || head.next == null) {
return head;
}
head.next = deleteDuplicates(head.next);
return head.val == head.next.val ? head.next : head;
}
}