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 ...
随机推荐
- oracle入门(4)——少而常用的命令
[本文介绍] 本文将介绍使用oracle的常用命令,不是”大全“,但少而实用. 下面的命令都是在项目用到的才记录下来的,如果以后用到其他的,会不断更新. [命令介绍] 描述: 命令: [数据库] ...
- java 程序cpu100%问题
找到java应用进程 ID即 java_id 找到该 java_id对应的CPU占用比较大的线程 ID即 thread_id 使用jdk自带jstack工具打印跟该线程相关的堆栈信息 [root@pv ...
- 转:使用log4net完成程序异常日志记录(使用SQLite数据库记录和普通文本记录)
http://www.cnblogs.com/kyo-yo/archive/2010/06/11/use-log4net-to-log-exception.html 在前端时间开发的时候由于需要将异常 ...
- 450. Delete Node in a BST
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...
- 【工具】PC端调试手机端 Html 页面的工具
一.概述 有一个项目需要在手机端显示一个 web 页面,而每次把应用 launch 后,从手机端看比较麻烦,因此搜罗了几种在 PC 端调试手机端页面的工具. 二.工具 http://fonkie.it ...
- LLServer--》对LevelDB的应用
http://code.google.com/p/llserver/ 查看libs path的路径 LD_DEBUG=libs /usr/bin/llserver -h
- Calendar的那些神坑
参考我的博客:http://www.isedwardtang.com/2017/08/31/java-calendar-bug/
- nohup- Shell后台运行
&方式: Unix/Linux下一般想让某个程序在后台运行,很多都是使用 & 在程序结尾来让程序自动运行.比如我们要运行mysql在后台: /usr/local/my ...
- SQL-ALTER-change和modify区别
ALTER 对于列的应用: 1.更改列名 格式:CHANGE old_col_name new_col_name column_definition 保留old和new列名 ...
- MyBatis正在爬的坑
换了份工作,开始接触Mybatis,开一篇文章记录一下自己遇到的坑 2018-06-20 今天遇到了一个问题,编好的sql语句在数据库可以执行但是写到程序里边就GG,什么问题呢?一直纠结在程序哪里写错 ...