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