Remove Duplicates from Sorted List
Description
Given a sorted linked list, delete all duplicates such that each element appear only once.
Example 1:
Input: 1->1->2 Output: 1->2
Example 2:
Input: 1->1->2->3->3 Output: 1->2->3
Solution(javascript)
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
const deleteDuplicates = (head) => {
const aux = (current) => {
if (!current || !current.next) {
return head
}
if (current.val === current.next.val) {
current.next = current.next.next
return aux(current)
}
return aux(current.next)
}
return aux(head)
}