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