LeetCode OJ——Remove Duplicates from Sorted List
http://oj.leetcode.com/problems/remove-duplicates-from-sorted-list/
链表的去重,要考虑链表的本质。
#include <iostream>
using namespace std; struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
}; class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
if(!head)
return head; ListNode *currNode = head->next;
ListNode *lastInList = head; //记录新列表中最后一个节点 while(currNode)
{
if(currNode->val == lastInList->val)
{
currNode = currNode->next;
if(currNode==NULL)
lastInList->next = NULL;
continue;
}
else
{
lastInList->next = currNode;
lastInList = currNode;
currNode = currNode->next;
} }
return head;
}
}; int main()
{
Solution myS;
ListNode *n1 = new ListNode(); ListNode *n2 = new ListNode();
ListNode *n3 = new ListNode();
ListNode *n4 = new ListNode();
ListNode *n5 = new ListNode();
n1->next = n2;
n2->next = n3;
n3->next = n4;
n4->next = n5; myS.deleteDuplicates(n1);
return ;
}
LeetCode OJ——Remove Duplicates from Sorted List的更多相关文章
- LeetCode OJ Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- 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 ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [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] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)
[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...
- [LeetCode] 83. Remove Duplicates from Sorted List 移除有序链表中的重复项
Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...
随机推荐
- python 监控日志
#需求: #1.每分钟监控服务器日志,ip请求超过200次的,加入黑名单 #1.读文件,获取到每行的内容 open readlines # 178.210.90.90 - - [04/Jun/2017 ...
- Python 成长之路
Python roadmap python 基础 ... 内置常用函数.三元运算.递归 迭代器和生成器 模块和常用的模块 面向对象 对向对象进阶 网络编程 并发编程 ... 数据库 MySQL pym ...
- 在SCIKIT中做PCA 逆变换 -- 新旧特征转换
PCA(Principal Component Analysis)是一种常用的数据分析方法.PCA通过线性变换将原始数据变换为一组各维度线性无关的表示,可用于提取数据的主要特征分量,常用于高维数据的降 ...
- eclipse使用技巧的网站收集——转载(二)
写代码离不开文本编辑器,看代码也离不开,iar和keil编辑和阅读简直一般般了,因此使用eclipse可以看看代码,提高效率.网上有几个博客的文章,这里收集一下,以备忘. 以下文章转载自:http:/ ...
- POJ:1904-King's Quest
King's Quest Time limit15000 ms Case time limit2000 ms Memory limit65536 kB Description Once upon a ...
- kettle-单表增量同步
目标:利于kettle实现单表增量同步,以时间为判断条件 背景:源表:db1.q1 (2w条数据) 目标表:db2.q2(0条数据) 表结构: CREATE TABLE `q1` ( `ID` bi ...
- vi 编辑器命令
插入命令 a append after the cursor A append after the current line i insert before the cursor I insert b ...
- Ext.js给form加背景图片
{ iconCls: 'zyl_icons_showdetail', tooltip: '查看', handler: function(gridView, rowIndex, colIndex) { ...
- Python文件处理、函数的基本应用
可读可写: r+t:可读.可写 w+t:可写.可读with open('b.txt','w+t',encoding='utf-8') as f: print(f.readable()) p ...
- 在Asp.net MVC中添加一个全局的异常处理的过滤器及Log4Net的使用
1:捕获异常新建一个异常处理的类MyExceptionAttribute捕获异常信息. //写到日志中.多个线程同时操作一个文件,造成文件的并发,这时用队列 public static Queue&l ...