(LeetCode 83)Remove Duplicates from Sorted Lists
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.
题目要求:
给一有序链表,删除重复的结点,使得每个元素只出现一次。
解题思路:
1、从头到尾遍历链表,如果前后两个结点相同,则将第一个结点指向它的下下结点,并删除它的下个结点。
2、递归思想。
代码:
/**
* 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) {
ListNode *lst=head;
ListNode *del;
while(lst && lst->next){
if(lst->val==lst->next->val){
del=lst->next;
lst->next=lst->next->next;
delete(del);
}
else
lst=lst->next;
}
return head;
}
};
/**
* 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;
if(head->val==head->next->val)
head=deleteDuplicates(head->next);
else
head->next=deleteDuplicates(head->next);
return head;
}
};
(LeetCode 83)Remove Duplicates from Sorted Lists的更多相关文章
- 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(83)题解: Remove Duplicates from Sorted List
https://leetcode.com/problems/remove-duplicates-from-sorted-list/ 题目: Given a sorted linked list, de ...
- 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- 【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...
- 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- C# 写 LeetCode easy #26 Remove Duplicates from Sorted Array
26.Remove Duplicates from Sorted Array Given a sorted array nums, remove the duplicates in-place suc ...
- [Leetcode][Python]26: Remove Duplicates from Sorted Array
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array ...
- 【LeetCode练习题】Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
- LeetCode OJ 82. Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
随机推荐
- Linux 内核编译步骤及配置详解
前言 Linux内核是操作系统的核心,也是操作系统最基本的部分. Linux内核的体积结构是单内核的.但是他充分采用了微内核的设计思想.使得虽然是单内核.但工作在模块化的方式下.并且这个模块可以 ...
- 小程序获取当前页面路径url
getCurrentPages()[0].route
- SecureCRT远程连接Linux服务器及相关配置
这里的连接采用的是SSH2协议,关于SSH2协议可百度 一.连接不上可能的原因 1)服务器防火墙iptables /etc/init.d/iptables stop 2)SSH服务有问题 3)客户端到 ...
- WP8.1 VS iOS VS Android全方面大比拼
众所周知,苹果的OS和谷歌的Android系统都有着相对成熟的设计和较好的用户体验,而随着WP8.1的发布,微软WP系统在交互方面也有了很多改进和提升,而今天小编便为大家全面对比一下这三大系统. ...
- 用显微镜观察cpu芯片内部
1. 先找到一块Intel公司的奔三(Pentium III)Coppermine芯片,主频800MHZ,生产于2000年.(我查了一下,网上的报价现在是15~30元人民币/块.) 下面是这块CPU的 ...
- Java实现归并排序(转)
Java实现归并排序 本文转自https://www.cnblogs.com/of-fanruice/p/7678801.html 归并排序 (merge sort) 是一类与插入排序.交换排序.选 ...
- JavaScript对于函数的调用及原理
<js> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>< ...
- 阿里云服务器IIS启用HTTPS协议(转)
https://www.cnblogs.com/randytech/p/7017188.html
- 字符串转换成JSON的三种方式
采用Ajax的项目开发过程中,经常需要将JSON格式的字符串返回到前端,前端解析成JS对象(JSON ).ECMA-262(E3) 中没有将JSON概念写到标准中,但在 ECMA-262(E5) 中J ...
- 第二章 Javac编译原理
注:本文主要记录自<深入分析java web技术内幕>"第四章 javac编译原理" 1.javac作用 将*.java源代码文件转化为*.class文件 2.编译流程 ...