给出一个升序排序的链表,删除链表中的所有重复出现的元素,只保留原链表中只出现一次的元素。
例如:
给出的链表为1→2→3→3→4→4→5, 返回1→2→5.
给出的链表为1→1→1→2→3, 返回2→3.
/*
* function ListNode(x){
* this.val = x;
* this.next = null;
* }
*/
/**
*
* @param head ListNode类
* @return ListNode类
*/
function deleteDuplicates( head ) {
// write code here
if(head == null || head.next == null){ return head}
const top = {}
top.next = head
var current = top
while(current.next && current.next.next){
if(current.next.val == current.next.next.val){
var val = current.next.val
while(current.next && current.next.val == val){
current.next = current.next.next
}
}else{
current = current.next
}
}
return top.next
}
module.exports = {
deleteDuplicates : deleteDuplicates
};
京公网安备 11010502036488号