Remove Duplicates from Sorted List II
来源:互联网 发布:php要看那些书籍 编辑:程序博客网 时间:2024/05/16 10:50
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
.
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public ListNode deleteDuplicates(ListNode head) { if(head==null || head.next==null) return head; ListNode curr=head; Map<Integer,Integer> map=new HashMap<Integer,Integer>(); while(curr!=null){ if(map.containsKey(curr.val)){ map.put(curr.val,map.get(curr.val)+1); } else{ map.put(curr.val,1); } curr=curr.next; } curr=head; ListNode prev=new ListNode(0); ListNode p=prev; prev.next=null; while(curr!=null){ if(map.get(curr.val)<=1){ prev.next=curr; curr=curr.next; prev=prev.next; prev.next=null; } else curr=curr.next; } return p.next; }}
0 0
- LeetCode: Remove Duplicates from Sorted List II
- 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
- Remove Duplicates from Sorted List II
- [leetcode] Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- Remove duplicates from sorted list II
- Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- [LeetCode]Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- [leetcode]Remove Duplicates from Sorted List II
- QTWebkit中的webkit/qt/api文档
- Matlab图像直方图相关函数
- 项目经理
- 二维码的生成细节和原理
- hive博客
- Remove Duplicates from Sorted List II
- 网络游戏服务器架构设计
- 5分钟弄懂Docker!
- boost.test 使用
- Matlab学习------逻辑型
- shell浅谈之四正则表达式
- DAO-DTO基础概念
- android二维码实现原理
- 时间序列模型预测