83. Remove Duplicates from Sorted List

Easy

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

Example 1:

Input: 1->1->2
Output: 1->2

Example 2:

Input: 1->1->2->3->3
Output: 1->2->3
package leetcode.easy;

public class RemoveDuplicatesFromSortedList {
@org.junit.Test
public void test() {
ListNode l11 = new ListNode(1);
ListNode l12 = new ListNode(1);
ListNode l13 = new ListNode(2);
l11.next = l12;
l12.next = l13;
l13.next = null;
print(l11); ListNode l21 = new ListNode(1);
ListNode l22 = new ListNode(1);
ListNode l23 = new ListNode(2);
ListNode l24 = new ListNode(3);
ListNode l25 = new ListNode(3);
l21.next = l22;
l22.next = l23;
l23.next = l24;
l24.next = l25;
l25.next = null;
print(l21); ListNode l1 = deleteDuplicates(l11);
print(l1);
ListNode l2 = deleteDuplicates(l21);
print(l2);
} private static void print(ListNode l) {
while (l != null) {
System.out.print(l.val);
if (l.next != null) {
System.out.print("->");
}
l = l.next;
}
System.out.println();
} public ListNode deleteDuplicates(ListNode head) {
ListNode current = head;
while (current != null && current.next != null) {
if (current.next.val == current.val) {
current.next = current.next.next;
} else {
current = current.next;
}
}
return head;
}
}

最新文章

  1. 观察者模式(Observer和Observable实现)
  2. 在线编辑器的使用总结(kindeditor , )
  3. MapReduce实例-基于内容的推荐(一)
  4. Edge Model
  5. Android中的sp与wp
  6. DuiLib(二)——控件创建
  7. js获取任意元素到页面的距离
  8. c# 根据窗口截图,合并图片
  9. jQuery图片延迟加载插件
  10. C++指针初始化总结
  11. jquery 中获取URL参数的方法
  12. 通过demo搞懂encode_utf8和decode_utf8
  13. uip UDPclient模式通信移植,当地port随机
  14. hdu 3683 Gomoku (模拟、搜索)
  15. ContentType是否大小写区分?
  16. jqgrid 分页 (基于ashx)
  17. DoxygenToolKit.vim 插件配置
  18. QEMU KVM Libvirt手册(8): 半虚拟化设备virtio
  19. sql语句格式化数字(前面补0)
  20. eosio.cdt:EOS智能合约工具集

热门文章

  1. Python经典算法-猴子吃桃-思路分析
  2. Spring Data JPA 提供的各种Repository接口作用
  3. 怎样运行jar包中的文件
  4. CF707D Persistent Bookcase 可持久化线段树
  5. 心跳(纯代码制作心形,animation动画)
  6. Crontab 定时任务格式参数
  7. qml 绘制高精地图之怀疑人生的加载速度
  8. fgetc,getc,fputc,putc,putchar,getchar
  9. Angular2日期格式化
  10. 我的公众号 - Old Artist