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

Example 1:

Input: 1->2->3->3->4->4->5
Output: 1->2->5

Example 2:

Input: 1->1->1->2->3
Output: 2->3
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head == null || head.next == null) {
return head;
} ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode cur = dummy;
// check the next two node
while(cur.next != null && cur.next.next != null) {
if (cur.next.val == cur.next.next.val) {
int num = cur.next.val;
while(cur.next != null && cur.next.val == num) {
cur.next = cur.next.next;
}
} else {
cur = cur.next;
}
}
return dummy.next;
}
}

最新文章

  1. 【转】深入浅出JavaScript之闭包(Closure)
  2. jsTree简单应用Demo
  3. linux安装pip报错
  4. csuoj 1120: 病毒
  5. shell条件测试
  6. POJ 1330 Nearest Common Ancestors(LCA模板)
  7. Session for Tornado(Redis) - 代码分享
  8. Android开发之搜Ya项目说明(3)
  9. oracle_面试题
  10. YII 常用路径总结
  11. UVa 1395 (最小生成树)
  12. java面向对象--类加载器及Class对象
  13. mysql数据库安装注意事项:
  14. thinkphp5.0 分页中伪静态的处理
  15. BZOJ1449[JSOI2009]球队收益&BZOJ2895球队预算——最小费用最大流
  16. linux+nginx+phpfpm 访问出现Access denied错误解决方案
  17. svn同步小脚本
  18. innerHTML、innerText和outerHTML、outerText的区别
  19. 单KEY业务,数据库水平切分架构实践 | 架构师之路
  20. 相似度与距离计算python代码实现

热门文章

  1. c++静态库和动态库的添加
  2. Python笔记_第四篇_高阶编程_高阶函数_3.sorted
  3. Java自学-泛型 泛型转型
  4. oi笔记——抽象的深度优先搜索
  5. Python批量重命名文件
  6. 将元素平分成差值最小的两个集合(DP)
  7. OpenVINO在linux下安装
  8. 关于本人:-D(必读)
  9. pix2pix-tf官方文档
  10. STL入门练习