Subject description:
Given a sorted list, delete all the duplicate elements, so that each element occurs only once.
Example 1:
输入: 1->1->2
输出: 1->2
Example 2:
输入: 1->1->2->3->3
输出: 1->2->3
Code:
JavaScript
- Relatively simple, familiar list.
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var deleteDuplicates = function(head) {
var cur = head
while(cur && cur.next) {
if (cur.next.val === cur.val) {
cur.next = cur.next.next
} else {
cur = cur.next
}
}
return head
};