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 ...
随机推荐
- HDU pog loves szh II (数的处理)
题意: 给一个序列,找出两个数字a和b(可以相等但不可相同),要求(a+b)%p的结果最大. 思路: 先将所有元素模p,再排序.要找出a和b,分两种情况,a+b>p和a+b<p.第一种,肯 ...
- (六)6.9 Neurons Networks softmax regression
SoftMax回归模型,是logistic回归在多分类问题的推广,即现在logistic回归数据中的标签y不止有0-1两个值,而是可以取k个值,softmax回归对诸如MNIST手写识别库等分类很有用 ...
- 【英语】Bingo口语笔记(52) - sleep系列
- LeetCode——Add Two Numbers
Question:You are given two linked lists representing two non-negative numbers. The digits are stored ...
- JSTL标签库大全
JSTL简介: 标准标签库JSTL的全名为:Java Server Pages Standard Tag Library. JSTL主要提供了5大类标签库: 1. 核心标签库: 为日常任务 ...
- Android Studio Check for Update
Android Studio 当前版本1.0.1, 官网新版本1.1.0, 通过 Check for Update...升级, 提示 Connection failed. Please check y ...
- ios 页面传值4种方式(一) 之全局变量
通用的是用代理的方式实现页面传值,但是有时候利用其它方式也可以很巧妙的解决问题,页面传值一共有4种方式: 1.使用全局变量, SharedApplication,定义一个变量来传递. 2.使用文件,或 ...
- js闭包用法
闭包 既保证了 内部函数的私有性 又可以向外公开 通过一个已有对象 向它注入属性 /** * 闭包 * 在函数中定义的函数,在外部使用 * 1.在函数内部定义的函数,在外部不能访问 */ functi ...
- Provider 错误 '80004005' 未指定的错误 的最终解决方法
今天在配置公司的香港WEB服务器Server2003系统,建好应用程序池后,发现远行程序经常出现下面的错误,刷新几下又可以,但过不了多久又是出现下面的错误!! 在网上查找相关问题得知,这是2003SP ...
- 把两个DataTable连接起来,相当于Sql的Inner Join方法
在C#中把两个DataTable连接起来,相当于Sql的Inner Join方法 作者:浪漫十一狼在下面的例子中实现了3个Join方法,其目的是把两个DataTable连接起来,相当于Sql的Inne ...