题目

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;
}
};

GitHub测试程序源码

LeetCode(82)Remove Duplicates from Sorted List的更多相关文章

  1. LeetCode(80)Remove Duplicates from Sorted Array II

    题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  2. LeetCode(28)-Remove Duplicates from Sorted Array

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  3. LeetCode(26) Remove Duplicates from Sorted Array

    题目 Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  4. LeetCode(83)Remove Duplicates from Sorted List

    题目 Given a sorted linked list, delete all duplicates such that each element appear only once. For ex ...

  5. leetCode练题——26. Remove Duplicates from Sorted Array

    1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates  ...

  6. 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 ...

  7. 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 ...

  8. <LeetCode OJ> 83. Remove Duplicates from Sorted List

    83. Remove Duplicates from Sorted List Total Accepted: 94387 Total Submissions: 264227 Difficulty: E ...

  9. Leetcode(82)-删除排序链表中的重复元素 II

    给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字. 示例 1: 输入: 1->2->3->3->4->4->5 输出: 1-&g ...

随机推荐

  1. 51Nod 1092 回文字符串

    最开始毫无头绪,然后参照了一位dalao的博客,思路是一个正序的字符串将其逆序,然后求最长公共子序列(LCS),emm也属于动态规划. #include <iostream> #inclu ...

  2. GCD = XOR(GCD XOR )

    首先没看懂XOR(于是百度了一下):异或,英文为exclusive OR,或缩写成xor.同时还有一个OR,于是一起看了一眼: 大意: 输入一个整数n,在1~n内,有多少对整数(a,b)满足GCD(a ...

  3. 清橙A1339. JZPLCM(顾昱洲)

    http://www.tsinsen.com/ViewGProblem.page?gpid=A1339 题解:https://blog.csdn.net/LOI_DQS/article/details ...

  4. DFS HDOJ 5348 Ponds

    题目传送门 题意:有一张无向图,度数小于2的点会被去掉,直到全都大于等于2,问连通块顶点数为奇数的权值和为多少 分析:首先DFS把度数小于2的vis掉,第二次DFS把属于同一个连通块的vis掉,检查是 ...

  5. VMwareworkstation 12安装

    由于这篇博客中有大量截图,一个一个上传太累了,所以请点击以下链接进行查看 百度云:http://pan.baidu.com/s/1bQxPSi 这里直接粘贴文字可以,但是详细的截图贴不出来!

  6. C# 部分命名规则

    接触C#开发已经四个月,整理下C#中的命名规则: 一:变量的命名规则(和Java相似) 1.变量名由字母.数字.下划线组成 2.变量名开头只能以字母.下划线开头,不能以数字开头 3.区分大小写 4.命 ...

  7. poj1724 ROADS

    题意: N个城市,编号1到N.城市间有R条单向道路.每条道路连接两个城市,有长度和过路费两个属性.Bob只有K块钱,他想从城市1走到城市N.问最短共需要走多长的路.如果到不了N,输出-12<=N ...

  8. canvas基础绘制-倒计时(上)

    效果: html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  9. VBA 连接sql server的用法

    cnnstr = "Provider=sqloledb;Data Source=192.211.21.8;Initial Catalog=pub;UID=账号;PWD=密码" VB ...

  10. MFC技术积累——基于MFC对话框类的那些事儿5

    4. 菜单 4.1 弹出菜单 本节主要讲解如何在主对话框的指定区域内通过鼠标右击来弹出一个菜单选项.最终效果图如图4.1. 如图4.1鼠标只能在指定区域(图中深色区域)内右击时弹出菜单,在指定区域外点 ...