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 ...
随机推荐
- 进程与线程(2)- python实现多进程
python 实现多进程 参考链接: https://morvanzhou.github.io/tutorials/python-basic/multiprocessing/ python中实现多进程 ...
- 微信小程序开发常见的拉起外部地图软件进行导航的功能
<view class="dh" bindtap="mapNavigation" data-addr="{{address}}"> ...
- 转--v$session & v$process各字段的说明【转载】
Oracle 动态性能表 v$session & v$process 整理自google出来的网络资源.google是个好东东.没有google我会心神不宁. v$session SADDR: ...
- 479 Largest Palindrome Product 最大回文数乘积
你需要找到由两个 n 位数的乘积组成的最大回文数.由于结果会很大,你只需返回最大回文数 mod 1337得到的结果.示例:输入: 2输出: 987解释: 99 x 91 = 9009, 9009 % ...
- 74LVC2G241双缓冲3态驱动器
- Windows API函数大全二
4. API之打印函数 AbortDoc 取消一份文档的打印 AbortPrinter 删除与一台打印机关联在一起的缓冲文件 AddForm 为打印机的表单列表添加一个新表单 AddJob 用于获取一 ...
- mysql对库,表及记录的增删改查
破解密码 #1.关闭mysqlnet stop mysqlmysql还在运行时需要输入命令关闭,也可以手动去服务关闭 #2.重新启动mysqld --skip-grant-tables跳过权限 #3m ...
- 洛谷2017 5月月赛R1
我只想说面对这种难度的题目就是冲着20%的数据暴力... 分数:40+20+36.1+38+0+19 T1 签到题 III 题目背景 pj组选手zzq近日学会了求最大公约数的辗转相除法. 题目描述 类 ...
- mongoDB学习初步总结
What? 最受欢迎的非关系型数据库之一.面向文档的数据库,在存储乎数据方面与关系型数据库有着本质的区别. Why? 简单易用 对多变的业务需求,适应性强于SQL型DB 性能 复制 索引 分片 丰富的 ...
- swift 泛型的类型约束
总结: 1.类型约束只能添加到泛型参量上面 2.关联类型是泛型参量: 3.关联类型可以通过 协议.关联类型名称的形式引用: func allItemsMatch<C1: Container, C ...