给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。
示例 1:
输入: 1->2->3->3->4->4->5
输出: 1->2->5
示例 2:
输入: 1->1->1->2->3
输出: 2->3
链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii
class Solution { public ListNode deleteDuplicates(ListNode head) { if(head ==null) return null; if(head.next==null) return head; ListNode s=new ListNode(0) ; //哨兵 s.next = head; ListNode pre =s; ListNode curr =head; ListNode tmp =curr.next; if(tmp.next ==null ) { if(curr.val==tmp.val) { return null; }else { return head; } } while(curr!=null) { while(tmp!=null&&curr.val!=tmp.val) { pre =pre.next; curr=curr.next; tmp=tmp.next; } if(tmp==null) break; if(tmp.next ==null) { pre.next=tmp.next; return s.next; } while(tmp.next!=null&&tmp.val==tmp.next.val) { tmp=tmp.next; } pre.next=tmp.next; curr =tmp.next; if(curr!=null) { tmp=curr.next; } if(tmp==null) break; } return s.next; } }