LeetCode(82)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.
分析
该题目与LeetCode 83题目类似,都是删除链表中重复元素的题目。
该题目要求只保存不重复的结点!
详细见代码!
AC代码
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (head == NULL || head->next == NULL)
return head;
ListNode * p = head;
while (p->next != NULL && p->val == p->next->val)
{
p = p->next;
}
//保存结点
ListNode *r = p->next;
//如果头结点是重复的
if (p != head){
while (head != r)
{
ListNode * tmp = head;
head = head->next;
free(tmp);
}
return deleteDuplicates(head);
}
//否则递归处理接下来的结点
head->next = deleteDuplicates(head->next);
return head;
}
};
LeetCode(82)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(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 List
题目 Given a sorted linked list, delete all duplicates such that each element appear only once. For ex ...
- 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 ...
- <LeetCode OJ> 83. Remove Duplicates from Sorted List
83. Remove Duplicates from Sorted List Total Accepted: 94387 Total Submissions: 264227 Difficulty: E ...
- Leetcode(82)-删除排序链表中的重复元素 II
给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字. 示例 1: 输入: 1->2->3->3->4->4->5 输出: 1-&g ...
随机推荐
- css width
转载:http://blog.csdn.net/dddddz/article/details/8631655
- python浅拷贝深拷贝
copy_list=list[:] 得到的是浅拷贝,即只能顶层拷贝,里面的嵌套不会复制一份. a = [0, [1, 2], 3] b = a[:] a[0] = 8 a[1][1] = 9 请问现 ...
- JAVA常用设计模式(静态化调用和实例化调用的区别,编辑可见 )
用newInstance()与用new是区别的,区别在于创建对象的方式不一样,前者是使用类加载机制,后者是创建一个新类,且newInstance()只能调用无参构造函数. 最大的区别在于内存.静态方法 ...
- Selenium中验证码处理
验证码的主要实现方法 1.读取方式:在服务器目录下保存制作好的图片文件.然后在web页面上让用户识别,这种方式完全可以通过URL来破解图片的地址,通过图片地址可以间接的知道图片表示的验证码是什么所以这 ...
- 使用 Realm 和 Swift 创建 ToDo 应用
原文出处: HOSSAM GHAREEB 译文出处:Prayer’s blog(@EclipsePrayer) 智能手机的快速发展的同时,涌现出了很多对开发者友好的开发工具,这些工具不仅使得开发变 ...
- 笔记《精通css》第2章 选择器,注释
第2章 选择器,注释 1.常用选择器(id选择器,类选择器,类型选择器,后代选择器,伪类选择器(文档结构之外)) 通用选择器(*{ }) 高级选择器(子选择器,相邻同胞选择器,属性选择器) ...
- HTML的历史与历史遗留问题
1. <style type="text/css"> 从前,HTML的设计者认为以后应该还会有其他样式,不过如今我们已经醒悟,事实表明,完全可以只使用<style ...
- Arduino中数据类型转换 float/double转换为char 亲测好使,dtostrf()函数
如何轻松玩转Arduino单片机,当我在进行数据转换的时候,遇到了问题,尝试了C语言和C++中的好多函数,都没有达到将float型数据转换为char型的目的.苦苦查阅资料后,终于找到了一个大神级函数! ...
- php 生成饼状图,折线图,条形图 通用类 2
生成饼状图,折线图,条形图通用的php类,这里使用的是国外的 HighCharts,前台页面别忘了调用HighCahrt js HighCharts中文网站 http://www.hcharts. ...
- codevs 5438 zbd之难题(水题日常)
时间限制: 1 s 空间限制: 1000 KB 题目等级 : 白银 Silver 题目描述 Description zbd想要一个计算器,请你编一个计算器. 输入描述 Input Descrip ...