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

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

解题思路:

修改上题代码即可,JAVA实现如下:

public ListNode deleteDuplicates(ListNode head) {
if (head == null || head.next == null)
return head;
ListNode temp = head.next;
if (temp.val == head.val) {
while (temp.val == head.val) {
temp = temp.next;
if (temp == null) {
head.next = null;
return head;
}
}
head.next = temp;
}
temp = head.next;
ListNode last = head;
while (temp != null && temp.next != null) {
if (temp.val != temp.next.val) {
last.next = temp;
temp = temp.next;
last = last.next;
continue;
}
last.next = temp;
last=last.next;
while (temp.val == last.val) {
temp = temp.next;
if (temp == null) {
last.next = null;
return head;
}
}
}
last.next = temp;
return head;
}

最新文章

  1. 关于基本类型值和引用类型值以及Vue官方API的array.$remove(reference)
  2. 前端scss的使用及gulp发布方式
  3. sql语句Group By用法-转载
  4. php 经典分页(推荐和laypage配合)
  5. iOS 键盘的隐藏
  6. Django的列表反序
  7. IE=EmulateIE8和IE=IE8的区别
  8. <postfix邮件服务下mysql的升级>
  9. 一步一步学数据结构之n--n(Prim算法)
  10. pandas教程1:pandas数据结构入门
  11. Lua编写wireshark插件初探——解析Websocket上的MQTT协议
  12. BootStrap 常用控件总结
  13. Spring生命周期 Constructor > @PostConstruct > InitializingBean > init-method
  14. CentOS6.5 安装vncserver实现图形化访问
  15. AutoPostBack
  16. python-GIL、死锁递归锁及线程补充
  17. 微服务架构实践 - 你只懂docker与spring boot就够了吗?
  18. AIX上打包排除某些文件/文件夹
  19. Linux C 重定向简单范例
  20. 前端基础-jQuery的内容之选择器

热门文章

  1. LeakCanary——直白的展现Android中的内存泄露
  2. flash+xml无法显示中文的解决办法
  3. mac 安装 word2016并破解
  4. 微信小程序 - 下载图片并且显示进度
  5. Java排序算法(三):直接插入排序
  6. SQL检索语句及过滤语句
  7. iphone手机分辨率--持久维护
  8. JavaScript 文件操作方法详解
  9. getOutString 输出弹出字符串
  10. ordinal parameter mismatch