Skip to main content

Leetcode - 83. Remove Duplicates from Sorted List

· One min read

https://leetcode.com/problems/remove-duplicates-from-sorted-list/

Solution 1

/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var deleteDuplicates = function (head) {
if (!head) {
return null;
}

let curr = head;
let next = head.next;

while (next) {
if (curr.val === next.val) {
curr.next = next.next;
} else {
curr = curr.next;
}
next = next.next;
}

return head;
};