存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除链表中所有存在数字重复情况的节点,只保留原始链表中 没有重复出现 的数字。
返回同样按升序排列的结果链表。力扣原文链接
示例 1:
输入:head = [1,2,3,3,4,4,5]
输出:[1,2,5]
示例 2:
输入:head = [1,1,1,2,3]
输出:[2,3]
解题思路:
遍历链表,当前节点与next节点对比,不想等进入下一个节点,相等从重复节点开始遍历,找到不相等的节点 找到后将当前节点next指向不相等节点
var deleteDuplicates = function(head) {
if(!head)return head
let emptyList=new ListNode(-1,head)
let cur=emptyList, repeatNode
while(cur.next){
debugger
if(cur.next.next&&cur.next.val==cur.next.next.val){
repeatNode=cur.next.next
while(repeatNode&&repeatNode.val==cur.next.val)repeatNode=repeatNode.next;
cur.next=repeatNode
}else{
cur=cur.next
}
}
return emptyList.next
};