Leetcode----- 82.Remove Duplicates from Sorted List II
来源:互联网 发布:泰剧app软件下载 编辑:程序博客网 时间:2024/06/07 01:37
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given 1->2->3->3->4->4->5
, return 1->2->5
.
Given 1->1->1->2->3
, return 2->3
.
给定一个排序的链表,删除所有具有重复数字的节点,从原始列表中只留下不同的结点。
代码如下:
public class RemoveDuplicatesfromSortedListII { public static ListNode deleteDuplicates(ListNode head) { if(head == null || head.next == null) { return head; } ListNode ret = new ListNode(0); ListNode node = ret; ListNode pre = head; ListNode point = head.next; while(point != null) { if(pre.val == point.val) { while(point.next != null && point.next.val == pre.val) { point = point.next; }pre = point.next;ret.next = pre;point = point.next; }else { ret.next = pre; pre = pre.next; ret = ret.next; } if(point != null) { point = point.next; } } return node.next; }public static void main(String[] args) {ListNode l10 = new ListNode(1);ListNode l11 = new ListNode(1);ListNode l12 = new ListNode(1);ListNode l13 = new ListNode(1);ListNode l14 = new ListNode(1);ListNode l15 = new ListNode(1);ListNode l16 = new ListNode(1);l10.next = l11;l11.next = l12;l12.next = l13;l13.next = l14;l14.next = l15;l15.next = l16;l16.next = null;ListNode node = deleteDuplicates(l10);while(node != null) {if(node.next == null) {System.out.println(node.val);}else{System.out.print(node.val +"->");}node = node.next;}}}
阅读全文
0 0
- LeetCode: Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List II
- LeetCode: Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List II
- [leetcode] Remove Duplicates from Sorted List II
- [LeetCode] Remove Duplicates from Sorted List II
- [leetcode] Remove Duplicates from Sorted List II
- [LeetCode]Remove Duplicates from Sorted List II
- [leetcode]Remove Duplicates from Sorted List II
- LeetCode-Remove Duplicates from Sorted List II
- [leetcode] Remove Duplicates from Sorted List II
- LeetCode - Remove Duplicates from Sorted List II
- LeetCode | Remove Duplicates from Sorted List II
- LeetCode:Remove Duplicates from Sorted List II
- 【leetcode】Remove Duplicates from Sorted List II
- Leetcode: Remove Duplicates from Sorted List II
- leetcode Remove Duplicates from Sorted List II
- [LeetCode] Remove Duplicates from Sorted List II
- 矩形图片怎么转换成圆形的图片
- 吃豆子
- Android中监听来电
- java -classpath 与 -jar
- 实验四 Linux系统管理
- Leetcode----- 82.Remove Duplicates from Sorted List II
- python——三道贪心算法例题
- 正确使用Qt多线程
- OkHttp3源码解析04-失败重连
- Adobe Dreamweaver CC 2018 Mac版 v18.0.0中文版下载
- 常用的凸优化方法
- Dubbo的使用详解
- Python模块的安装路径
- bzoj2724 [Violet 6]蒲公英 分块