83. Delete the sort the list of repeating elements
Given a sorted list, delete all the duplicate elements, so that each element occurs only once.
Example 1:
Input: 1-> 1-> 2
Output: 1-> 2
Example 2:
Input: 1-> 1-> 2-> 3-> 3
Output: 1-> 2-> 3
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head == null || head.next == null){
return head;
}
head.next = deleteDuplicates(head.next);
if(head.val == head.next.val) head = head.next;
return head;
}
}