LeetCode OJ: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.
简单的链表去重而已啊,遍历一边就实现了:
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode * p = head;
if(p == NULL || p->next == NULL)
return p;
ListNode * prev = p;
p = p->next;
while(p!=NULL){
if(p->val == prev->val){
prev->next = p->next;
}else{
prev = p;
}
p = p->next;
}
return head;
}
};
下面这个实际上比上面那个要快一点,上面那个是遇到一个删掉一个,这个是遇到一连串相同的就一起删掉,java写的,runtime比上面又不小的提高,代码如下:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head == null) return null;
ListNode helper = new ListNode(-1);
helper.next = head;
ListNode p = head;
while(p.next!=null){
if(p.val == p.next.val){
ListNode tmp = p.next;
while(tmp.next != null){
if(tmp.next.val == tmp.val){
tmp = tmp.next;
}else
break;//找到最后一个和前面相同的节点
}
p.next = tmp.next;
tmp.next = null;
}else{
p = p.next;
}
}
return helper.next;
}
}
LeetCode OJ: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 e ...
- [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项 II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- LeetCode OJ Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- LeetCode OJ——Remove Duplicates from Sorted List
http://oj.leetcode.com/problems/remove-duplicates-from-sorted-list/ 链表的去重,要考虑链表的本质. #include <ios ...
- leetCode 82.Remove Duplicates from Sorted List II (删除排序链表的反复II) 解题思路和方法
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>
LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)
https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...
- [LeetCode] 82. Remove Duplicates from Sorted List II_Medium tag: Linked List
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinctnumbe ...
随机推荐
- mysql 内置功能 函数 date_format函数
创建数据库db12 create database db12 charset=utf8; use db12; 准备表和记录 CREATE TABLE blog ( id INT PRIMARY KEY ...
- nodejs升级的两种方法
方法一: 用n升级nodejs # 全局安装n$ npm install -g n# 升级到最新稳定版$ n stable # 升级到最新版$ n latest# 升级到定制版$ n v7.10.0# ...
- Kettle-1-安装配置
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/wl101yjx/article/details/32921691 写在前面一: 数据仓库ETL工具有 ...
- dplyr快速入门
RStudio Blog 介绍dplyr 包已发布 (Introducing dplyr), 此包将原本 plyr 包中的 ddply() 等函数进一步分离强化, 专注接受dataframe对象, 大 ...
- SQL基础一
一.什么是SQL? SQL是结构化查询语言 SQL使我们有能力访问数据库 SQL是一种ANSI的标准计算机语言 二.SQL能做什么? SQL 面向数据库执行查询 SQL 可从数据库取回数据 SQL 可 ...
- Java中String.valueOf、toString、(String)的区别
原文地址http://blog.csdn.net/yangzhaomuma/article/details/51173138 原文地址https://www.cnblogs.com/xhyouyou/ ...
- 连接postgresql
# psycopg2 engine=create_engine('postgresql+psycopg2://scott:tiger@localhost/mydatabase')# python 连 ...
- python+selenium+API
一.浏览器操作 1.浏览器最大化 driver.maximize_window() #将浏览器最大化显示 2.设置浏览器宽.高 driver.set_window_size(480, 800)#设置浏 ...
- Python 中的条件运算符
一.概述 在 C.Java 等语言中,有一种常见的条件运算符,又叫"三目运算符". 详情参见 [条件运算符-wikipedia][https://zh.wikipedia.org/ ...
- js踩过的一些坑
参考我的博客:http://www.isedwardtang.com/2017/08/29/js-bug/