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 ...
随机推荐
- 项目中使用的ajax异步读取数据结构设计
设计稍微复杂了一点,完成ajax读取功能涉及到了很多页面.虽然如此,但感觉比较灵活. 和传统方法唯一的区别在于多了一层数据容器控件,里面提供了显示数据的HTML元素及相应的JS方法. 这样数据控件指生 ...
- 【解题报告】[动态规划] CodingTrip - 携程编程大赛 (预赛第一场)- 聪明的猴子
原题: 聪明的猴子 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Problem D ...
- xampp无法打开phpmyadmin解决方案
如果设置了apache的端口号(如8890),那么不可以用自带的admin按钮打开,而是要加上端口(如localhost:8890/phpmyadmin/)
- std::sort引发的core
#include <stdio.h> #include <vector> #include <algorithm> #include <new> str ...
- sqlite3使用简介(内含解决sqlite内存的方法)
一.使用流程 要使用sqlite,需要从sqlite官网下载到三个文件,分别为sqlite3.lib,sqlite3.dll,sqlite3.h,然后再在自己的工程中配置好头文件和库文件,同时将dll ...
- 设置VMWARE通过桥接方式使用主机无线网卡上网(zz)
环境:WIN7旗舰版,台式机,U盘无线上网卡. 虚拟软件:VMware9.0,虚拟系统:CentOS6.4 需要实现虚拟机以独立机形式工作和上网. 先介绍一下VMware网络设置的三种方式 1 Hos ...
- 细雨学习笔记:JMeter 的主要测试组件总结
1. 测试计划是使用 JMeter 进行测试的起点,它是其它 JMeter 测试元件的容器. 2. 线程组代表一定数量的并发用户,它可以用来模拟并发用户发送请求.实际的请求内容在Sampler中定义, ...
- 【转】linux之ln命令
转自:http://www.cnblogs.com/peida/archive/2012/12/11/2812294.html ln是linux中又一个非常重要命令,它的功能是为某一个文件在另外一个位 ...
- list 容器 排序函数.xml
pre{ line-height:1; color:#f0caa6; background-color:#2d161d; font-size:16px;}.sysFunc{color:#e54ae9; ...
- Spring依赖注入 --- 模拟实现
Spring依赖注入 --- 模拟实现 面向接口编程,又称面向抽象编程, 数据库如果发生更改,对应的数据访问层也应该改变多写几个实现,需要用谁的时候在service里new谁就可以了面向抽象编程的好处 ...