I'm trying to delete duplicate nodes in a singly linked list: [1,1,2] would return [1,2].
So I would check if head.val == head.next
, and if so, set head.val = null
. However, in Javascript, null apparently returns an object. So now my output would be [[],1,2] instead of the desired [1,2].
var deleteDuplicates = function(head) {
if (!!(head && head.next)) {
if (head.val === head.next.val) {
head.val = null;
}
}
return head;
};
How can I get it to return [1,2]?
P.S. this is a coding question from Leetcode - I'm just wondering how I can set the value to nothing instead of null, which is an object.