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

For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.

这道题特别简单,其实关于链表的题都没有什么技巧,就是一堆的指针指来指去.

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. ListNode* deleteDuplicates(ListNode* head) {
  12. ListNode *pre,*now,*Head;
  13. if(!head||!head->next)return head;
  14. Head=new ListNode(-);
  15. Head->next=head;
  16. pre=Head;
  17. now=head;
  18. while(now&&now->next)
  19. {
  20. if(now->val == now->next->val)
  21. {
  22. while(now->next && now->val == now->next->val)
  23. {
  24. now=now->next;
  25. }
  26. pre->next=now->next;
  27. now=now->next;
  28. }
  29. else
  30. {
  31. pre=now;
  32. now=now->next;
  33. }
  34. }
  35. head=Head->next;
  36. delete(Head);
  37. return head;
  38. }
  39. };

  

Remove Duplicates from Sorted List II——简单的指针问题的更多相关文章

  1. 【链表】Remove Duplicates from Sorted List II(三指针)

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

  2. 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)

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

  3. Remove Duplicates from Sorted List II

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

  4. 【leetcode】Remove Duplicates from Sorted Array II

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

  5. 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

  6. 48. Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II

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

  7. 【LeetCode练习题】Remove Duplicates from Sorted List II

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

  8. Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II

    以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...

  9. LeetCode之“链表”:Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II

    1. Remove Duplicates from Sorted List 题目链接 题目要求: Given a sorted linked list, delete all duplicates s ...

随机推荐

  1. 解题:APIO 2008 免费道路

    题面 我们发现我们可以很容易知道最终完成的生成树中有多少鹅卵石路,但是我们不好得到这棵生成树的结构,所以我们尽量“谨慎”地完成生成树·,最好是一点点加到我们要达到的标准而不是通过删掉一些东西来完成 我 ...

  2. HDU 2586 倍增法求lca

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. 数据压缩算法之哈夫曼编码(HUFFMAN)的实现

    HUFFMAN编码可以很有效的压缩数据,通常可以压缩20%到90%的空间(算法导论).具体的压缩率取决于数据的特性(词频).如果采取标准的语料库进行编码,一般可以得到比较满意的编码结果(对不同文件产生 ...

  4. 关于java 获取 html select标签 下拉框 option 文本内容 隐藏域

    在HTML中从多选下拉框中提取已选中选项的文本内容到后台,被这个问题难倒了. demo.jsp文件 <select id="selecttype" name"typ ...

  5. 1.Spring揭秘--Ioc容器

    1.Ioc即控制反转,假设一个类需要依赖另外一个类,在最初始的做法就是创建那个依赖类的对象,然后使用这个类提供的功能,如果创建这个依赖类的职责交给Ioc Service Provider去做,那么这就 ...

  6. 算法专题-STL篇

    这篇文章着重记录c++中STL的用法.主要粗略的介绍其用法,以知识点的形式呈现其功能,不会深入源码分析其工作原理. 排序和检索. sort(a,a+n),对a[0]往后的n个元素(包括a[0])进行排 ...

  7. 图论&搜索:K短路-启发式搜索

    判断第k短路的权值是否小于T 直接把队友的代码拿过来了,一定很经典 #include <iostream> #include <queue> #include <cstr ...

  8. vijos 1288 箱子游戏 计算几何

    背景 hzy是箱子迷,他很喜欢摆放箱子,这次他邀请zdq,skoier一起来玩game... 描述 地板上有一个正方形的大箱子和许多三角型的小箱子.所有的小箱子都在大箱子里面,同时,一些三角形的小箱子 ...

  9. 修改tomcat的Response Hearder 头中的Server信息

    如图: Server: Apache-Coyote/1.1 这个信息给入侵者提供了一定的指示作用.为了安全起见,要求更改这个信息.那么我们就来修改一下试试,非常简单,只要在Connector中添加se ...

  10. 01 DIV+CSS 固定页面布局

    本文讲解使用DIV+CSS布局最基本的内容,读完本文你讲会使用DIV+CSS进行简单的页面布局. DIV+CSS布局中主要CSS属性介绍: Float: Float属性是DIV+CSS布局中最基本也是 ...