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