LeetCode(83)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.
分析
删除链表中反复元素结点。
该题目本质非常easy。仅仅需一次遍历。须要注意的是。要释放删除的结点空间。
AC代码
/**
* 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 == NULL || head->next == NULL)
return head;
ListNode *p = head, *q = p->next;
while (p && q)
{
if (p->val == q->val)
{
ListNode *t = q;
p->next = q->next;
q = q->next;
//释放要删除的结点空间
delete t;
}
else{
p = p->next;
q = q->next;
}//elif
}//while
return head;
}
};
LeetCode(83)Remove Duplicates from Sorted List的更多相关文章
- LeetCode(80)Remove Duplicates from Sorted Array II
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode(82)Remove Duplicates from Sorted List
题目 Given a sorted linked list, delete all duplicates such that each element appear only once. For ex ...
- LeetCode(28)-Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- LeetCode(26) Remove Duplicates from Sorted Array
题目 Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- (LeetCode 83)Remove Duplicates from Sorted Lists
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- leetCode练题——26. Remove Duplicates from Sorted Array
1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates ...
- LeetCode(4)Median of Two Sorted Arrays
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- LeetCode之“链表”:Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II
1. Remove Duplicates from Sorted List 题目链接 题目要求: Given a sorted linked list, delete all duplicates s ...
- [LC]83题 Remove Duplicates from Sorted List(删除排序链表中的重复元素)(链表)
①英文题目 Given a sorted linked list, delete all duplicates such that each element appear only once. Exa ...
随机推荐
- iOS开发之手势gesture详解(二)
与其他用户界面控件交互 UIControl子类会覆盖parentView的gesture.例如当用户点击UIButton时,UIButton会接受触摸事件,它的parentView不会接收到.这仅适用 ...
- poj 2253(kruskal)
Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34968 Accepted: 11235 Descrip ...
- LeetCode OJ--Unique Paths *
https://oj.leetcode.com/problems/unique-paths/ 首先,转换成一个排列组合问题,计算组合数C(m+n-2) (m-1),请自动想象成上下标. class S ...
- AC日记——[SCOI2010]游戏 bzoj 1854
1854: [Scoi2010]游戏 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 4938 Solved: 1948[Submit][Status] ...
- Windows Builder(图形化界面的利器)For Eclipse 3.7
工欲善其事,必先利其器——孔子(春秋)<论语·卫灵公> 今天闲逛论坛的时候,发现了Eclipse 的很好的插件,是关于做图形界面的. 如果想做桌面应用软件,交互界面有点复杂的时候,自己手动 ...
- POJ 1860 Currency Exchange 最短路+负环
原题链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Tota ...
- ApplicationContext介绍
如果说BeanFactory是Spring的心脏,那么ApplicationContext就是完整的身躯了.ApplicationContext由BeanFactory派生而来,提供了更多面向实际应用 ...
- Cookie安全与CSRF和XSS攻击知识点收集
个人简单理解: 1.XSS最简单的理解就是可以在表单提交的内容上嵌入JS执行代码,然后页面渲染的时候没有过滤时会自动执行这个脚本. 2.CSRF可以理解为当你登录了京东,浏览器上保存了你登录的Cook ...
- iOS -- SKTextureAtlas类
SKTextureAtlas类 继承自 NSObject 符合 NSCodingNSObject(NSObject) 框架 /System/Library/Frameworks/SpriteKi ...
- 仰视源代码,实现strcpy
编程实现字符串的拷贝,不能用库函数. 一般的刚開始学习的人也许能写出来.可是要写的非常完美那就须要基本功了. char* strcpy(char* strDest, const char* strSr ...