leetcode 83:Remove Duplicates from Sorted List
2015-11-30 21:00
483 查看
题目:
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given
Given
思路:
这题很简单,直接上代码。
时间复杂度:O(n)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(!head || !head->next) return head;
ListNode *first=head,*second=head;
while(second->next)
{
if(second->val==second->next->val)
{
second=second->next;
continue;
}
first->next=second->next;
first=first->next;
second=first;
}
first->next=second->next;
return head;
}
};
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.
思路:
这题很简单,直接上代码。
时间复杂度:O(n)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(!head || !head->next) return head;
ListNode *first=head,*second=head;
while(second->next)
{
if(second->val==second->next->val)
{
second=second->next;
continue;
}
first->next=second->next;
first=first->next;
second=first;
}
first->next=second->next;
return head;
}
};
相关文章推荐
- scrollView中嵌套ListView,ListView不能滚动的问题
- WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping。请添加一个名为 jquery (区分大小写)的 ScriptResourceMapping。
- ASCII码表16进制版
- 51单片机复习题
- NEUOJ 1660 (容斥+矩阵)
- STL的copy真是是做的了极致的效率
- 1086. Tree Traversals Again
- 学习记录:基于adaboost的人脸检测
- 在Visual Studio中使用VueJS时,不可以用 v-bind 的简写 : 及 v-on的简写 @
- 记一次图片优化经历
- JDBC操作TimesTen
- 欢迎使用CSDN-markdown编辑器
- C语言位运算符:与、或、异或、取反、左移和右移
- 转载AFNetworking源码解析(四)
- ArcGIS AddIn开发之:固定距离+节点生成点
- homerHEVC代码阅读(12)——基础结构之其他结构
- fibonacci数列(五种)
- Acm - 隔壁老王买酒问题
- LeetCode 36 Valid Sudoku(有效数独)
- js延迟加载