给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。

示例 1:

输入: 1->2->3->3->4->4->5
输出: 1->2->5

示例 2:

输入: 1->1->1->2->3
输出: 2->3 思路:(1)因为头结点可能被删除,所以要定义头结点;(2)定义两个指针,一个指向前驱结点,另一个指针指向前驱节的下一个结点,并不断找到值相等的结点。
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) { if(!head || !head->next)
return head; ListNode* dummy = new ListNode(-); ListNode* p = dummy;
dummy->next = head;
while(p->next)
{
ListNode* cur = p->next; while(cur->next && cur->val == cur->next->val)
cur = cur->next; if(cur != p->next)
p->next = cur->next;
else
p = p->next; } return dummy->next;
}
};

    

Remove Duplicates from Sorted ListII的更多相关文章

  1. [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  2. [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  3. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  4. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  5. Leetcode-83 Remove Duplicates from Sorted List

    #83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that ...

  6. Remove Duplicates from Sorted List II

    Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...

  7. Remove Duplicates From Sorted Array

    Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...

  8. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  9. 26. Remove Duplicates from Sorted Array

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

随机推荐

  1. Vue中splice的使用

    转载:https://blog.csdn.net/xiha_zhu/article/details/80449339 splice(index,len,[item])它也可以用来替换/删除/添加数组内 ...

  2. Web前端性能优化常见面试题

    一般说来,web前端指网站业务逻辑之前的部分,包括浏览器加载.网站视图模型.图片服务.CDN服务等,主要优化手段有浏览器访问.使用反向代理才.CDN等.1.减少http请求,合理浏览器缓存 2.启用压 ...

  3. Kaldi如何统计data数据集

    统计时长 wav-to-duration scp:data/train/wav.scp ark,t:- 2>/dev/null|awk 'BEGIN{SUM=0}{SUM+=$2}END{pri ...

  4. git修改文件夹名字

    git mv -f oldfolder newfolder git add -u newfolder (-u选项会更新已经追踪的文件和文件夹) git commit -m "changed ...

  5. centos7 安装.net core的方法

    安装: sudo yum install libunwind libicu curl -sSL -o dotnet.tar.gz https://go.microsoft.com/fwlink/?li ...

  6. Django REST Framework API Guide 05

    本节大纲 1.Serializer fields 2.Serializer relations Serializer fields 1.serializer 字段定义在fields.py文件内 2.导 ...

  7. 源码学习之mybatis

    1.先看看俩种调用方式 public static void main(String[] args) { SqlSessionFactory sqlSessionFactory; SqlSession ...

  8. Django中的csrf基础了解

    简介 django为用户实现防止跨站请求伪造的功能,通过中间件 django.middleware.csrf.CsrfViewMiddleware 来完成.而对于django中设置防跨站请求伪造功能有 ...

  9. [转载]tmux常用快捷键

    Hello World 窗口管理只是 tmux 功能的一小部分,另一个很有用的功能就是,连接到远程主机之后,一旦断开,那么当前账户登录的任务就被取消了,但是使用 tmux 可以在断开之后继续工作,下次 ...

  10. OVS-----CentOS7.2上安装OVS软件

    1.安装依赖包: yum -y install make gcc openssl-devel autoconf automake rpm-build redhat-rpm-config yum -y ...