Remove Duplicates from Sorted List @LeetCode
/**
* Remove Duplicates from Sorted List
*
* 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.
*/
public class S83 { public static void main(String[] args) {
ListNode n1 = new ListNode(1);
ListNode n2 = new ListNode(1);
n1.next = n2;
ListNode n3 = new ListNode(2);
n2.next = n3; ListNode x = deleteDuplicates(n1);
x.print();
} public static ListNode deleteDuplicates(ListNode head) {
ListNode base = head; // base 为每次被比较的对象
if(head==null || head.next == null){
return head;
}
ListNode cur = head.next; // cur为每次去比较的对象 while(base!=null && cur!=null){
if(base.val == cur.val){ // 重复了就跳过去
base.next = cur.next;
}else{ // 不重复就更新base
base = base.next;
}
cur = cur.next; // 更新cur
} return head;
}
}
Remove Duplicates from Sorted List @LeetCode的更多相关文章
- Remove Duplicates from Sorted Array [LeetCode]
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- Remove Duplicates From Sorted Array leetcode java
算法描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...
- Remove Duplicates from Sorted List leetcode java
题目: Given a sorted linked list, delete all duplicates such that each element appear only once. For e ...
- Remove Duplicates From Sorted Array
Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...
- [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- LeetCode:Remove Duplicates from Sorted List I II
LeetCode:Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such t ...
随机推荐
- 编译安装apache+php(加常见问题解决)
[编译apache]./configure --prefix=/usr/local/lamp/httpd -with-apr=/usr/local/apr -with-apr-util=/usr/lo ...
- BZOJ 1076 奖励关
注意几点: 1.为什么要逆推?由此状态可以轻易算出彼状态是否可行,而彼状态却无法轻易还原为此状态. 2.为什么可以逆推?假设时光倒流了....23333 3.注意位运算的准确,大胆写方程. #incl ...
- BZOJ 1010 玩具装箱
斜率优化. 事实上是选一个大于某个数的最小斜率.维护下凸壳. #include<iostream> #include<cstdio> #include<cstring&g ...
- Java 直线、多段线画板 PaintJFrame (整理)
package demo; import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; impor ...
- Mac Maven java_home错误
当maven装好之后出现 $ mvn -versionError: JAVA_HOME is not defined correctly. We cannot execute /usr/libexec ...
- Heritrix源码分析(二) 配置文件order.xml介绍(转)
本博客属原创文章,欢迎转载!转载请务必注明出处:http://guoyunsky.iteye.com/blog/613412 本博客已迁移到本人独立博客: http://www.yun5u. ...
- js判断是否是pc
//判断是否是pc function IsPC() { var userAgentInfo = navigator.userAgent; var Agents = new Array("An ...
- NPOI 2.0导出word(docx格式)
大名鼎鼎的NPOI用来导出EXCEL的文章园子里面有很多,可是用来导出WORD文档的文章大都含糊不清,最近刚好完成一个导出WORD文档的需求,在此分享下. NPOI里面认为word文档的最基本的结构是 ...
- C#-gdi绘图,双缓冲绘图,Paint事件的触发
一. 画面闪烁问题与双缓冲技术 1.1 导致画面闪烁的关键原因分析: 1 绘制窗口由于大小位置状态改变进行重绘操作时 绘图窗口内容或大小每改变一次,都要调用Paint事件进行重绘操作,该操作会使画面 ...
- Android实例] android获取web服务器端session并验证登陆
传统网页实现用户登陆一般采用session或cookie记录用户基本信息又或者两者结合起来使用.android也可以采用session实现用户登陆验证并记录用户登陆状态时的基本信息,session是在 ...