原题
给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。
示例 1:
输入: 1->2->3->3->4->4->5
输出: 1->2->5
示例 2:
输入: 1->1->1->2->3
输出: 2->3
解法
思想
双指针吧
代码
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode sentinel = new ListNode(0);
ListNode cur = sentinel;
while(head!=null){
if(head.next!=null && head.next.val == head.val){
do{
head = head.next;
}while(head.next!=null && head.next.val == head.val);
head = head.next;
}else{
ListNode node = new ListNode(head.val);
cur.next = node;
cur = node;
head = head.next;
}
}
return sentinel.next;
}
}
原创文章,作者:彭晨涛,如若转载,请注明出处:https://www.codetool.top/article/leetcode82-%e5%88%a0%e9%99%a4%e6%8e%92%e5%ba%8f%e9%93%be%e8%a1%a8%e4%b8%ad%e7%9a%84%e9%87%8d%e5%a4%8d%e5%85%83%e7%b4%a0ii/